Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_IocpProactor: Acquire lock in previously missed methods #87

Merged
merged 1 commit into from
Oct 3, 2023

Conversation

dnadlinger
Copy link
Contributor

@dnadlinger dnadlinger commented Oct 1, 2023

For the same reason that recv(), send(), accept() and connect()
need to acquire the lock, all the other operations need to as well.

This also shows a design limitation in this approach w.r.t.
maintainability (silent failure when new operations are added);
it would be preferable to either reproduce the windows_events
implementation in its entirety, or use proxying via composition
to ensure methods not made thread-safe cannot be invoked.

Finally, the _poll implementation seems to have further issues:
it is not clear to me that the _stopped_serving handling is
thread-safe, _unregistered is not handled, and it seems
preferable not to hold the lock when calling
GetQueuedCompletionStatus. I restricted this pull request to
addressing the straight-forward omission of wrappers for
ease of review, however.

For the same reason that recv(), send(), accept() and connect()
need to acquire the lock, all the other operations need to as well.

This also shows a design limitation in this approach w.r.t.
maintainability (silent failure when new operations are added);
it would be preferable to either reproduce the windows_events
implementation in its entirety, or use proxying via composition
to ensure methods not made thread-safe cannot be invoked.

Finally, the _poll implementation seems to have further issues:
it is not clear to me that the _stopped_serving handling is
thread-safe,  _unregistered is not handled, and it seems
preferable not to hold the lock when calling
GetQueuedCompletionStatus.
@hosaka
Copy link
Collaborator

hosaka commented Oct 3, 2023

Thanks for the PR. It makes sense to wrap the missing methods with a mutex, happy to merge this.

On the _poll implementation, from my understanding the idiom WeakRef is None should be thread safe. Whether that implies that obj in WeakSet is safe or not, I am not sure. Looking at weakref docs:

# r is a weak reference object
o = r()
if o is None:
    # referent has been garbage collected
    print("Object has been deallocated; can't frobnicate.")
else:
    print("Object is still live!")
    o.do_something_useful()

Using a separate test for “liveness” creates race conditions in threaded applications; another thread can cause a weak reference to become invalidated before the weak reference is called; the idiom shown above is safe in threaded applications as well as single-threaded applications.

The WeakSet.__iter__ itself uses and _IterationGuard which has a comment about it being "relatively" thread-safe, however this SO post suggests otherwise.

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.

None yet

2 participants