fix: stop closing the ping/receive wait handles while they are still … - #12
Open
andreion1ca wants to merge 1 commit into
Open
andreion1ca wants to merge 1 commit into
andreion1ca wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13.
Fixes the crash reported in alttester/AltTester-Server#206.
releaseCommonResources()closed_pongReceivedand_receivingExitedout from under threads that were already insideReset/Set/WaitOneon them — a use-after-dispose of an OS wait handle.On the server, that window is opened by the session sweep:
Sweep()→InactiveIDs→broadping()pings live connections, and a connection being torn down at that moment releases the handles underneath the ping. BothAccessViolationExceptionreports in alttester/AltTester-Server#206 land on the first sweep tick, 60s after startup, each one shortly after a rejected connection ("Too many apps connected.") was closed.Changes:
releaseCommonResourcesdrops the references without closing the handles. Every reader snapshots the field into a local first, so a handle still in use stays valid until that last reference goes away andSafeWaitHandle's finalizer releases it. A lock was the alternative, but it can't cover both fields:_pongReceivedis only touched under_forPing, while_receivingExitedis used by the receive loop and bycloseHandshake, neither of which holds that lock.closeHandshake,processPongFrame(which had acatch (NullReferenceException)papering over exactly this), and servercloseHandshake(which read_receivingExitedtwice — null check, then wait).Sweep()clears_sweepingin afinally, so a throw can't leave the flag stuck attrueand silently disable the sweep for the rest of the process's life.Verified by building the netstandard2.0 DLL and running AltTester-Server's test suite against it: 133/133 passing. That exercises the normal connect/close paths, not the narrow ping-vs-teardown window itself.
Paired with alttester/AltTester-Server's own fix, which turns the sweep off (
KeepClean = false) — a pong that doesn't arrive withinWaitTime(1s) is treated as a dead session, which is what a backgrounded mobile app or a paused editor looks like. This PR is the hardening for every other consumer of the library.