Skip to content

Review native device-removal candidate with upstream PR 16 - #3

Draft
shurkanTwo wants to merge 12 commits into
workerThreadfrom
maintenance/device-removal-review
Draft

shurkanTwo wants to merge 12 commits into
workerThreadfrom
maintenance/device-removal-review

Conversation

@shurkanTwo

@shurkanTwo shurkanTwo commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

terminal WPD error -> mark session gone -> detach notifications
nested Open       -> reject departed session; preserve open count
fresh Open        -> clear gone only after success
interrupted image -> finish task and capture as failed
property copy     -> preserve absent defaults; initialize owned pointers

Preserve @totoantibes's original upstream PR #16 commit 5a26bd79e44615d5f1b21a529496e5ca02f8e556, with follow-up fixes for removal state, callback ownership/cookies, nonblocking notification queuing, command responses and partial-image failure. Production-object tests also exposed two pre-existing property-copy crashes, corrected here. Original commit history and legal notices are preserved.

This is a separate experimental candidate: native file version 1.0.1.23, package 1.0.1.23-device-removal-test.1. The regular/Latest 1.0.1.22 release and workerThread remain separate. No modified ASCOM wrapper is included.

Evidence

  • Before: Nested Open could clear device loss without creating a session; callbacks could outlive their owner; interrupted first or later images could leave capture incomplete.
    After: Final Windows CI passes x86 and x64 production-object tests, including 48 terminal transport/result failures, nested/fresh reopen, late callbacks, missing events, partial capture failure, 64 concurrent shutdown cycles and busy-worker callback liveness. The fake endpoint uses real Windows WPD collections; no physical camera is involved.
  • Before: Copying a property with no default and copying into reused storage caused x86/x64 access violations in the missing-default reproducer and the ownership reproducer.
    After: Both cases pass using the production property objects. These defects are not claimed to explain upstream's physical-removal crashes.
  • Before: Build success alone did not establish that distributed binaries and complete source matched.
    After: Both architectures pass DLL loading, 28 exports, two local dependencies, 20 lens definitions, seven allocation regression cases and diagnostic-tool checks. Packaging validates architecture/provenance; the complete source ZIP rebuilds and passes on Windows outside Git. SHA256SUMS.txt and SOURCE.txt accompany the ZIPs. The PR workflow compiles temporary merge commit fe7424cdab5d320a604224c77246da0fa5b4966f, recorded in BUILD.json and SOURCE_REVISION.

Download native-release-packages from the successful run while signed into GitHub. It contains both architecture ZIPs, matching complete source and checksums. Follow installation/rollback instructions and hardware acceptance steps. The separate review record records corrected Standards and Spec findings; neither final review has unresolved findings.

Credits: @dougforpres for the original project, @totoantibes for upstream PR dougforpres#16, and @shurkanTwo for follow-up fixes, builds, tests and documentation.

Merge Danger

Door: Two-way

Keep this PR draft and out of workerThread until physical-camera acceptance and any crash evidence have been reviewed. Back up replaced native files and verify loaded module paths before testing.

Blast Radius: Native

Removal handling, callback lifetime/queuing, property copying and capture failure behavior change. The upstream author reports remaining abnormal exits after the original patch. Physical USB teardown, camera timing, image fidelity and ASCOM/NINA integration remain unverified. Busy-worker callbacks can drop notifications and rely on existing polling; hardware tests must check capture completeness. This candidate does not establish safe hot unplug, transparent reconnect or resolution of upstream issue dougforpres#15.

totoantibes and others added 11 commits August 24, 2026 16:45
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants