-
Notifications
You must be signed in to change notification settings - Fork 5.2k
avoid NRE in MsQuicConnection ConnectAsync #56283
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
Changes from all commits
f9e3284
dbc0830
6f3901f
dccf730
4c5e6de
1d906fb
9426974
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,6 +227,7 @@ private static uint HandleEventConnected(State state, ref ConnectionEvent connec | |
|
|
||
| state.Connected = true; | ||
| state.ConnectTcs!.SetResult(MsQuicStatusCodes.Success); | ||
| state.ConnectTcs = null; | ||
| } | ||
|
|
||
| return MsQuicStatusCodes.Success; | ||
|
|
@@ -576,7 +577,8 @@ internal override ValueTask ConnectAsync(CancellationToken cancellationToken = d | |
| throw new Exception($"Unsupported remote endpoint type '{_remoteEndPoint.GetType()}'."); | ||
| } | ||
|
|
||
| _state.ConnectTcs = new TaskCompletionSource<uint>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| // We store TCS to local variable to avoid NRE if callbacks finish fast and set _state.ConnectTcs to null. | ||
| var tcs = _state.ConnectTcs = new TaskCompletionSource<uint>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| try | ||
| { | ||
|
|
@@ -600,7 +602,7 @@ internal override ValueTask ConnectAsync(CancellationToken cancellationToken = d | |
| throw; | ||
| } | ||
|
|
||
| return new ValueTask(_state.ConnectTcs.Task); | ||
| return new ValueTask(tcs.Task); | ||
| } | ||
|
|
||
| private ValueTask ShutdownAsync( | ||
|
|
@@ -683,9 +685,10 @@ private static uint NativeCallbackHandler( | |
|
|
||
| if (state.ConnectTcs != null) | ||
| { | ||
| state.ConnectTcs.SetException(ex); | ||
| state.ConnectTcs = null; | ||
| // This is opportunistic if we get exception and have ability to propagate it to caller. | ||
| state.ConnectTcs.TrySetException(ex); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the change I was hinting at when asking about the race condition.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I can do better fix. Instead of checking state of the task I can simply store TaskCompletionSource in ConnectAsync to local variable and return that. There is really no reason to drag it past Connect event. |
||
| state.Connection = null; | ||
| state.ConnectTcs = null; | ||
| } | ||
| else | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.