Skip to content

(3/3) Fix concurrency webcam windows#684

Merged
hexbabe merged 10 commits intopion:mainfrom
hexbabe:fix-concurrency-webcam-windows
Apr 21, 2026
Merged

(3/3) Fix concurrency webcam windows#684
hexbabe merged 10 commits intopion:mainfrom
hexbabe:fix-concurrency-webcam-windows

Conversation

@hexbabe
Copy link
Copy Markdown
Contributor

@hexbabe hexbabe commented Feb 10, 2026

Summary

  • Per-thread COM initialization: CoInitializeEx was only called once in init(). Go goroutines can run on any OS thread, so Open() and VideoRecord() now call CoInitializeEx on their current thread before making COM calls. COINIT_MULTITHREADED is idempotent so repeated calls are safe.
  • Mutex and idempotent Close: Added sync.Mutex protecting shared camera state (cam, closed, ch, done) to prevent races between concurrent Open/Close/VideoRecord/Properties calls. Close snapshots fields under the lock, nils them out, then tears down outside the lock. Double close is a no-op.
  • Done channel for callback unblock: imageCallback previously did a blocking send on ch. If no reader was consuming, this blocked the DirectShow streaming thread forever. Added a done channel to the select so Close can unblock it.
  • C-allocated frame buffer: The DirectShow BufferCB callback writes into a frame buffer from a non-Go thread. Previously this buffer was a Go heap slice whose backing array the GC could relocate, causing silent memory corruption. Now allocated with C.malloc and freed explicitly in Close.
  • C++ bugfixes: Fixed delete cam->props to delete[] cam->props (mismatched new[]/delete). Removed unused LPOLESTR name variable in listResolution.

Stacked on top of #683

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.13%. Comparing base (9a3d191) to head (145e352).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #684   +/-   ##
=======================================
  Coverage   42.13%   42.13%           
=======================================
  Files          86       86           
  Lines        5186     5186           
=======================================
  Hits         2185     2185           
  Misses       2839     2839           
  Partials      162      162           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hexbabe hexbabe force-pushed the fix-concurrency-webcam-windows branch from 1d80925 to 354b5c4 Compare February 11, 2026 16:23
@hexbabe hexbabe changed the title Fix concurrency webcam windows (3/3) Fix concurrency webcam windows Feb 11, 2026
@hexbabe hexbabe force-pushed the fix-concurrency-webcam-windows branch from 354b5c4 to 56b68cb Compare February 11, 2026 17:38
@hexbabe hexbabe force-pushed the fix-concurrency-webcam-windows branch from 56b68cb to 1e3f733 Compare March 19, 2026 21:27
@hexbabe hexbabe marked this pull request as ready for review March 19, 2026 22:11
@hexbabe hexbabe requested a review from JoTurk March 24, 2026 17:49
@hexbabe
Copy link
Copy Markdown
Contributor Author

hexbabe commented Apr 13, 2026

Hi @JoTurk this will be among my final contributions to mediadevices for a while :,)

could you take a look this week?

hexbabe added 9 commits April 13, 2026 10:43
…tyBag, with fallback to display name. Update LDFLAGS in camera_windows.go to include oleaut32 library.
- Introduced mutex for thread safety in camera struct to protect shared resources.
- Updated Open method to initialize done channel and manage camera state.
- Enhanced Close method to ensure proper cleanup and prevent double closing.
- Improved resolution listing logic in camera_windows.cpp by ensuring media types are freed correctly.
- Changed memory deletion from single to array deletion for camera properties.
@hexbabe hexbabe force-pushed the fix-concurrency-webcam-windows branch from 145e352 to 430c428 Compare April 13, 2026 14:43
@JoTurk
Copy link
Copy Markdown
Member

JoTurk commented Apr 13, 2026

this will be among my final contributions to mediadevices for a while

:(

thank you for all the work you've done.

could you take a look this week?

Yeah, I'll try it to test it today or tomorrow, I have a 2nd machine with Windows, So testing and reviewing these PRs is a lot easier for me now :)

Comment thread pkg/driver/camera/camera_windows.cpp
C.freeCamera(cam)
}

C.free(cbuf)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this run into a potential UAF if we have an already running imageCallback callback?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually safe because freeCamera calls IMediaControl::Stop(), which blocks until any in-flight BufferCB returns. close(done) before freeCamera ensures the callback can't deadlock in the select. So C.free(cbuf) only runs after all callbacks are done.

That said, I moved the RUnlock past the copy so the safety is obvious from the Go side without needing to know DirectShow's Stop() semantics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

((IMediaControl*)cam->mediaControl)->Stop();

@hexbabe hexbabe requested a review from JoTurk April 14, 2026 19:27
@hexbabe
Copy link
Copy Markdown
Contributor Author

hexbabe commented Apr 20, 2026

Hey @JoTurk could I get a second pass review here? TY

Copy link
Copy Markdown
Member

@JoTurk JoTurk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@hexbabe hexbabe merged commit 3d4fcea into pion:main Apr 21, 2026
@hexbabe hexbabe deleted the fix-concurrency-webcam-windows branch April 21, 2026 13:25
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