In the previous Kinect Lab article, I mentioned that the first real run streamed, then failed during USB shutdown and could not reopen. I promised the diagnosis separately. In the logs, close reported LIBUSB_ERROR_NO_DEVICE, and a later open refused an interface that appeared to belong to a kernel driver.

A screenshot of the cloud said very little about either problem. For the viewer to be useful while I experimented, I needed to stop it and start it again without making reconnecting the cable part of the interface.

The investigation led into libfreenect2-metal, the native driver beneath my Rust application. I kept the viewer aside and compared isolated driver variants on the same hardware. There were two USB problems to separate, and removing an old shutdown delay also meant making the end of asynchronous processing explicit.

An interface that became available shortly afterwards

The driver resets the USB device during opening, then checks ownership before claiming its control/RGB and infrared interfaces. In the original code, one positive libusb_kernel_driver_active result was enough to refuse the claim.

The libusb contract returns 1 when a kernel driver is active and 0 when none is active.

But on the Aukey hub I was testing, a probe recorded these two lines after reset:

reset+0ms active0=1 active1=0
reset+100ms active0=0 active1=0

Later direct claims and releases of interfaces 0 and 1 succeeded without detaching a driver. The probe sampled at intervals, so it shows the interface clear by the next sample.

The Darwin backend in libusb 1.0.30 checks for a child of the interface in the IOService registry plane. I did not capture the identity of the transient child.

My patch adds up to ten retries, 50 ms apart, per interface on macOS. It retries only while the ownership query returns 1. An interface that remains owned still causes opening to fail, and a failed claim is still an error. Nothing is detached. Immediately available interfaces do not incur that wait.

Close was asking the sensor to reboot

With the readiness wait in the final candidate, opening succeeded on the Aukey hub where the original driver refused. On the Selore SEUC3303 hub, which opened without it, a readiness-only variant still failed at close.

The old macOS close path contained a workaround linked to an earlier libfreenect2 issue. Its comment says, “Kinect will disappear on Mac OS X regardless during close().” Before releasing the claimed interfaces, it sent a shutdown command and waited four seconds. These are the two relevant statements from that branch, with the error check included:

  if (!command_tx_.execute(ShutdownCommand(nextCommandSeq()), result))
    success = false;
  libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(4 * 1000));

The surrounding source comment already described the command as rebooting the device. The registry trace from my original-driver run showed the sensor disappearing during close and returning roughly 1.7 seconds later. Releasing interface 0 through the old handle then returned LIBUSB_ERROR_NO_DEVICE.

I compared that with an isolated variant that kept the readiness wait and also removed the deliberate reboot. On the same Selore setup, the interfaces released cleanly and the next open succeeded.

With the patched driver, I followed each of two closes with ten seconds of registry observation. Across 113 samples, the sensor never disappeared, contrary to the old comment, and the reopen between the two windows succeeded.

Waiting for the reboot to finish had not made the old interface handle usable again. The four seconds delayed the failing release. I removed the macOS reboot branch, keeping the other shutdown commands and the checks on interface release.

Removing a sleep leaves an ordering question

The four-second delay could also give background processing time to finish. Removing it raised a separate question: what guarantees that no processor is still using a frame listener when close detaches it?

The driver has asynchronous RGB and depth packet processors. Canceling USB transfers stops their input, but a packet may already be queued or being processed. The worker can still be delivering a frame or returning its packet buffer. An absence of new USB input does not establish that this work has completed.

I added a barrier after USB transfer cancellation in stop(), and before listener detachment in close(). The close path needs its own call because cleanup after a failed start may reach it without going through the normal streaming stop path. The pipeline method waits on its asynchronous RGB and depth processors when they exist.

At the processor level, the complete new method is small:

  void waitUntilIdle()
  {
    libfreenect2::unique_lock l(packet_mutex_);
    while(current_packet_available_)
    {
      WAIT_CONDITION(idle_condition_, packet_mutex_, l);
    }
  }

The worker implementation keeps the same mutex while processing its packet. It releases the packet buffer, clears current_packet_available_ and notifies the idle condition. A caller arriving during active processing first waits to acquire that mutex. A caller arriving while a packet is queued can acquire it, see pending work and wait on the condition variable. Returning from the barrier therefore covers buffer release as well as processing.

The order I need is: stop the producers, wait for queued and active work, then detach the listeners and release their resources. A longer sleep would only estimate the middle step.

USB submission stops and transfers are canceled before queued and active
processing drains. Only then are listeners detached, interfaces released
and USB storage freed.

The teardown order in the patched driver. The processing wait includes returning the final packet buffer; its duration depends on that work.

The wait has no timeout, and it must not be called from a processing callback. The API comment documents both constraints.

Giving the waits something to fail against

I wanted tests that could distinguish a correct wait from a fortunate schedule.

The committed USB claim tests compile the real USB control implementation with replacements for the ownership query and interface claim calls. They feed it transient ownership, persistent ownership and actual claim failures. A transient result must eventually permit a claim; persistent ownership must still refuse one. Becoming ready must not turn a later LIBUSB_ERROR_BUSY into success.

For processing, the regression tests deliberately hold the worker at different stages: before it processes a queued packet, inside processing, and inside buffer release. The barrier must remain blocked until the gate opens. Other cases cover an idle processor, rejected packets whose buffers still need returning, and repeated packet reuse.

Rebuilding the earlier standalone diagnostics against both revisions gives the same answer: the readiness diagnostic fails against the original one-shot check and passes against the patch. The listener model, run without the barrier, lets teardown overtake processing; with the real barrier it waits, including 200 further packet reuses.

The model does not reproduce a physical USB disconnect, and nothing here proves that the original close error came from a listener race.

Back through the real sensor

The hardware investigation was recorded on 3 November 2026, using an Apple M4 Pro, macOS 26.5.1, Kinect v2 firmware 4.0.3917.0, libusb 1.0.30 and Metal depth processing. The original driver revision was 600cef5. The final candidate completed ten capture/stop/close cycles on the Selore SEUC3303 hub, reopening the device each time. Active close, immediate close and open-without-streaming cases were also checked there. An injected infrared-start failure remained an error, while cleanup still closed successfully. The separate Aukey results are described in the live viewer article.

The browser provided a later check of the application using that driver on the Aukey hub. On 14 November, Debug and Release each ran for 60 seconds. The Debug interface returned to “idle”; the Release status explicitly reported error=null after Stop. Six additional short Start/Stop cycles ended idle with no error.

These runs supply the 60-second browser run and Release comparison that the previous article still listed as missing. Some short cycles still logged packet-loss warnings.

The fix is public in my driver fork, with regression tests and diagnostic follow-ups. I checked PR #32 on 15 November 2026; it remains open upstream. Older macOS versions, other firmware and physical unplug during streaming remain outside this validation.

I wanted Stop to close the sensor and let me start another capture. Opening needed a bounded readiness check. Closing needed to leave the device present and finish work whose lifetime extended beyond USB input. The two waits have different jobs: one gives an interface a chance to become available; the other must finish before I let its listeners go.

Comments, questions, or just a reaction?

Send an email to ~bounga/bounga.org-discuss@lists.sr.ht. It is public and archived, so other readers can follow along and answer too.