Repository navigation
Stop talking to a camera that Windows has taken away - #16
totoantibes wants to merge 1 commit into
Conversation
Unplugging a camera mid-session, or powering it off while connected, reliably takes the hosting application down - an access violation somewhere under the portable device stack. Reproduced repeatedly against 1.0.1.21 with an ILCE-7S on Windows 11, driver logging at /d1. Five separate defects contribute; each is fixed below and each was observed in isolation before and after. 1. The idle refresh polls a device that is not there. CameraWorker::Run manufactures a RefreshPropertiesTask every 50ms whenever the queue is empty, so that a photo taken with the camera's own shutter button is noticed. refreshSettings swallows the resulting exception and returns Success, so the loop immediately builds another. With the device gone that is an unbounded poll of absent hardware - 16 iterations over 13.2s in one capture, ending in the process dying. The worker now skips the idle refresh once the camera has stopped answering. Queued work still runs, and a queued task that succeeds clears the state. 2. downloadImage loops forever when no image comes back. The while (imageReady) condition is only re-evaluated inside if (image). When GetImage returns null - which is what happens when the camera vanishes mid-download - imageReady is never updated and the loop spins, asking a departed device for object info on every pass. Now breaks out. 3. Object info is parsed from a response that was never received. Camera::GetImageInfo builds an ObjectInfo from whatever Receive returned, including the empty message left by a failed send. The result is field after field read off the end of the buffer, each logged as "would extend beyond end of data" - 13 of them in one crash. Now returns null when the response is empty, and GetImage checks for it rather than dereferencing. 4. Nothing noticed the device had gone until each caller found out separately. Every call into the portable device stack passes through MTPDevice::IsSuccess and MTPDevice::InternalSend. The device is now flagged on the first HRESULT that says it is no longer connected (0x802A0002 and friends), and InternalSend refuses everything after that without touching WPD. One gate rather than a check in each caller, and no threshold to tune - the first such error is proof enough. Cleared on Open. 5. The device event registration outlives the device. This is the one that actually kills the process. WPD holds a pointer to DeviceEventHandler and calls into it while tearing the device down, roughly twelve seconds after the hardware disappears - long after the driver has stopped making calls of its own, which is why silencing the outbound paths above was not enough on its own. The registration is now dropped as soon as the device is known to be gone, while the objects are still valid, and the unregister is observed to succeed. Also: Close released IPortableDevice only when the close itself succeeded, which it cannot do on a device that has already gone - so the interface was held for the life of the process in exactly the case where letting go matters. It is now released either way, and the pointless round trip to absent hardware is skipped. Measured on the same rig, pulling the cable during a 2s exposure loop: before, the process died within a second every time; after, it survives the disconnect, stays up while unplugged, and reconnects when the camera returns. Not a complete fix. Three disconnect/reconnect cycles inside six minutes still ended in a crash, with the driver silent for eleven seconds beforehand and nothing in its log to point at. A single transient disconnect - the case that happens in practice when a cable is nudged - is handled.
|
Hi @totoantibes, thank you for the device-removal investigation. I have preserved your original commit This work remains experimental and is not included in the published native 1.0.1.22 release. That release incorporates your separate transfer-buffer fix from #17. Its manual installation and rollback instructions apply to 1.0.1.22, which does not claim to fix device-removal crashes. The separate candidate is 1.0.1.23-device-removal-test.1. Its Windows run passed x86/x64 production-object tests with 48 injected terminal-failure scenarios, packaging and a complete-source ZIP rebuild. Tests also exposed two pre-existing property-copy defects, corrected on the candidate branch; these are not established as the cause of the physical-removal crashes. The candidate remains draft and separate from the default branch and release. Physical USB teardown, capture/reconnection timing and ASCOM/NINA integration still need testing; passing the automated checks does not establish that the remaining crashes are resolved. Candidate-only packages, installation/rollback guidance and the hardware acceptance checklist are linked from the draft PR. |
When the camera disappears from USB mid-session, the driver keeps issuing WPD
calls against an
IPortableDevicethat is no longer backed by a device. Everycall fails with
0x802A0002(WPD_E_DEVICE_NOT_OPEN), the worker thread keepslooping on settings refreshes, and roughly 12-14 seconds later WPD calls back
into
DeviceEventHandlerwhile the interface is being torn down. That iswhere the host process dies, so the crash lands well after the unplug and does
not look related to it.
Relates to #15.
What this changes
Five separate places, all of the same shape - notice the device is gone, then
stop.
DevicegainsMarkGone()/IsGone()and a virtualOnGone(), so"the OS took this away" is a state the object carries rather than something
each caller rediscovers.
MTPDevice::InternalSendclassifies the failingHRESULT(
IsDeviceGoneResult) and, once gone, returns an empty response instead ofreissuing.
OnGone()callsStopNotifications()- this is the change thatactually stops the late callback.
CameraWorkerskips the settings refresh when the camera looksdisconnected and sleeps 500ms, instead of spinning on failing calls.
Cameracounts consecutive failed refreshes(
DISCONNECTED_REFRESH_FAILURES = 5) before declaring the camera gone, so asingle transient failure does not trip it.
GetImageInfonow returns null onan empty response and
GetImagenull-checks it.MTPDevice::Closereleases the interface even when the close pathfails, which previously leaked it on exactly the paths that matter here.
OnGone()deliberately does not releasem_device.MarkGone()isreached from inside
InternalSend, which is itself called with the interfacein use, so releasing there is a null-dereference on the way back out. Only
StopNotifications()is safe at that point.What was verified
Built and run against an ILCE-7S over 13 NINA sessions and ~3000 frames.
Before the change, removing the camera reliably took NINA down. After it, the
session survives removal, a 2.5 minute absence, and reconnection.
What was not
This does not make removal reliably survivable, and I would not describe it
as a fix for #15. Three of those sessions still ended abnormally at a
removal - no graceful-shutdown record, the log simply stops within ~2s of the
device going away. In all three the camera was idle, not mid-capture, which
rules out the "died mid-transfer" explanation I started with. I could not
capture a faulting module for those three, so I cannot say whether the
remaining failure is still inside this teardown path or somewhere else
entirely.
So: this removes one demonstrable crash cause and stops the driver making
calls it knows will fail. It is an improvement, not a cure, and it is offered
on that basis.