#1756·JUCE

[Bug]: Windows: destroying a MidiInput after its device is unplugged blocks for ~6.7 s (regression in 9.0.3)

Author: pob31Created Sep 28, 2026Updated Sep 28, 2026

Detailed steps on how to reproduce the bug

Since JUCE 9.0.3, on Windows with the default Win32 (WinMM) MIDI backend, destroying a MidiInput whose device has been removed blocks the calling thread for about 6.7 seconds. Apps usually do this on the message thread when MidiDeviceListConnection reports that the device has gone. The whole UI then freezes every time a MIDI controller is unplugged. JUCE 9.0.2 does the same close in about 100 ms.

  1. Install loopMIDI (it installs the teVirtualMIDI driver used below).
  2. Build and run this console app, configured with -DJUCE_DIR=<path to JUCE>.
  3. Run unplug_port.py. It creates a virtual MIDI input called "Probe port", keeps it for 3 s, then removes it.

main.cpp:

cpp
#include <juce_audio_devices/juce_audio_devices.h>
#include <iostream>

using namespace juce;

// Opens the MIDI input named "Probe port" and, when it disappears from the
// device list, destroys the MidiInput and prints how long that took.
struct Repro final : MidiInputCallback
{
    Repro()
    {
        open();

        connection = MidiDeviceListConnection::make ([this]
        {
            if (input != nullptr && ! isListed())
            {
                const auto start = Time::getMillisecondCounterHiRes();
                input.reset();
                std::cout << "destroyed MidiInput of a removed device in "
                          << Time::getMillisecondCounterHiRes() - start << " ms" << std::endl;
            }
            else if (input == nullptr)
            {
                open();
            }
        });
    }

    void open()
    {
        for (const auto& d : MidiInput::getAvailableDevices())
        {
            if (d.name == "Probe port" && (input = MidiInput::openDevice (d.identifier, this)) != nullptr)
            {
                input->start();
                std::cout << "opened " << d.name << std::endl;
                return;
            }
        }
    }

    static bool isListed()
    {
        for (const auto& d : MidiInput::getAvailableDevices())
            if (d.name == "Probe port")
                return true;

        return false;
    }

    void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override {}

    std::unique_ptr<MidiInput> input;
    MidiDeviceListConnection connection;
};

int main()
{
    ScopedJuceInitialiser_GUI init;
    Repro repro;
    MessageManager::getInstance()->runDispatchLoopUntil (60000);
    return 0;
}

CMakeLists.txt:

cmake
cmake_minimum_required(VERSION 3.22)
project(MidiCloseRepro VERSION 0.0.1 LANGUAGES C CXX)

add_subdirectory(${JUCE_DIR} juce)

juce_add_console_app(MidiCloseRepro)
target_sources(MidiCloseRepro PRIVATE main.cpp)
target_compile_features(MidiCloseRepro PRIVATE cxx_std_17)
target_compile_definitions(MidiCloseRepro PRIVATE
    JUCE_WEB_BROWSER=0 JUCE_USE_CURL=0 JUCE_MODAL_LOOPS_PERMITTED=1)
target_link_libraries(MidiCloseRepro PRIVATE juce::juce_audio_devices)

unplug_port.py (standard Python plus the teVirtualMIDI DLL):

python
# Creates a virtual MIDI input called "Probe port" with the teVirtualMIDI
# driver (installed by loopMIDI), keeps it for 3 s, then removes it.
import ctypes
import ctypes.wintypes as wt
import time

dll = ctypes.WinDLL("teVirtualMIDI64.dll")
CB = ctypes.WINFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(ctypes.c_ubyte), wt.DWORD, ctypes.c_void_p)
dll.virtualMIDICreatePortEx2.restype = ctypes.c_void_p
dll.virtualMIDICreatePortEx2.argtypes = [wt.LPCWSTR, CB, ctypes.c_void_p, wt.DWORD, wt.DWORD]
dll.virtualMIDIClosePort.argtypes = [ctypes.c_void_p]
ignore = CB(lambda *args: None)

port = dll.virtualMIDICreatePortEx2("Probe port", ignore, None, 65535, 1 | 12)  # PARSE_RX | INSTANTIATE_BOTH
print("port created")
time.sleep(3)
dll.virtualMIDIClosePort(port)  # the device disappears
print("port removed")
time.sleep(8)

Output, same program and script:

JUCE Output
9.0.2 destroyed MidiInput of a removed device in 104.457 ms
9.0.3 destroyed MidiInput of a removed device in 6686.49 ms

Where the time goes. We instrumented ~InputDevice() in juce_Midi_windows.cpp (9.0.3) for this close:

  • midiInStop() returns 6 (MMSYSERR_NODRIVER).
  • midiInReset() returns 6 (MMSYSERR_NODRIVER).
  • After the reset, all 32 headers still have MHDR_INQUEUE set, and none has WHDR_DONE.
  • Every MidiHeader::unprepare() gets MIDIERR_STILLPLAYING on all 10 tries: 32 × 10 × Thread::sleep (20), 6595 ms in total.
  • midiInClose() then returns 65 (MIDIERR_STILLPLAYING).

The waiting comes from aaee3f2c98 ("MIDI (Windows): Tear down inputs in the correct order"). unprepare() used to act only on headers with WHDR_DONE set. It now retries every header. When the driver is gone, the buffers are never handed back, so every header uses up all its retries.

Possible fix: skip the unprepare retries when midiInReset() fails, since nothing will be returned once the device is gone. Or keep the old WHDR_DONE check in unprepare(): now that the reset comes first, a successful midiInReset() has already marked every pending buffer as done.

What is the expected behaviour?

Destroying the MidiInput of a removed device returns promptly, as it did in 9.0.2 (about 100 ms), and doesn't block the message thread.

Operating systems

Windows

What versions of the operating systems?

Windows 11 Pro 25H2 (build 26200), with the in-box Windows MIDI Services (the virtual port appears through MIDISRV). JUCE built with the default MIDI flags (JUCE_USE_WINDOWS_MIDI_SERVICES=0, JUCE_USE_WINRT_MIDI=0), Visual Studio 2026, Release. We tested with virtual ports (teVirtualMIDI) only, not with a physical USB device.

Architectures

x86_64

Stacktrace

No crash. The time is spent in InputDevice::~InputDevice() → MidiHeader::unprepare() (juce_Midi_windows.cpp, lines 2635 and 2568 at 9.0.3).

Testing on the develop branch

The relevant code is unchanged on develop (a6aab3a6bd).