-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix in-process callback reclamation after connection close #2610
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c024b4
Fix in-process callback reclamation
stephentoub 092f08d
Address FFI teardown review findings
stephentoub c42c83c
Address callback teardown review
stephentoub c33405b
Format Node callback lifetime test
stephentoub 1e59819
Fix required build validation
stephentoub 9ac996b
Address .NET cleanup analysis
stephentoub eb01d91
Log rejected Go runtime shutdown
stephentoub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using System.Reflection; | ||
| using Xunit; | ||
|
|
||
| namespace GitHub.Copilot.Test.Unit; | ||
|
|
||
| public sealed class FfiRuntimeHostLifetimeTests | ||
| { | ||
| [Fact] | ||
| public void Dispose_Retains_Callback_Until_Connection_Close_Succeeds() | ||
| { | ||
| var allowClose = false; | ||
| var closeCalls = 0; | ||
| var shutdownCalls = 0; | ||
| var callbackReleaseCalls = 0; | ||
| Func<uint, bool> connectionClose = _ => | ||
| { | ||
| Interlocked.Increment(ref closeCalls); | ||
| return Volatile.Read(ref allowClose); | ||
| }; | ||
| Func<uint, bool> hostShutdown = _ => | ||
| { | ||
| Interlocked.Increment(ref shutdownCalls); | ||
| return true; | ||
| }; | ||
| Action releaseCallback = () => Interlocked.Increment(ref callbackReleaseCalls); | ||
|
|
||
| var hostType = typeof(CopilotClient).Assembly.GetType("GitHub.Copilot.FfiRuntimeHost", throwOnError: true)!; | ||
| var constructor = hostType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic) | ||
| .Single(candidate => candidate.GetParameters().Length == 8); | ||
| using var host = (IDisposable)constructor.Invoke( | ||
| [ | ||
| "test-runtime", | ||
| null, | ||
| null, | ||
| Array.Empty<string>(), | ||
| NullLogger.Instance, | ||
| connectionClose, | ||
| hostShutdown, | ||
| releaseCallback, | ||
| ]); | ||
|
|
||
| hostType.GetField("_connectionId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(host, (uint)21); | ||
| hostType.GetField("_serverId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(host, (uint)11); | ||
|
|
||
| host.Dispose(); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| Assert.Equal(0, Volatile.Read(ref callbackReleaseCalls)); | ||
| Assert.Equal(0, Volatile.Read(ref shutdownCalls)); | ||
|
|
||
| Volatile.Write(ref allowClose, true); | ||
| Assert.True( | ||
| SpinWait.SpinUntil( | ||
| () => Volatile.Read(ref callbackReleaseCalls) == 1 | ||
| && Volatile.Read(ref shutdownCalls) == 1, | ||
| TimeSpan.FromSeconds(5)), | ||
| "Deferred native cleanup did not complete."); | ||
|
|
||
| var closeCallsAfterCleanup = Volatile.Read(ref closeCalls); | ||
| host.Dispose(); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| Thread.Sleep(50); | ||
|
|
||
| Assert.Equal(closeCallsAfterCleanup, Volatile.Read(ref closeCalls)); | ||
| Assert.Equal(1, Volatile.Read(ref callbackReleaseCalls)); | ||
| Assert.Equal(1, Volatile.Read(ref shutdownCalls)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Dispose_Does_Not_Retry_Terminal_Host_Shutdown_Failure() | ||
| { | ||
| var closeCalls = 0; | ||
| var shutdownCalls = 0; | ||
| var callbackReleaseCalls = 0; | ||
|
|
||
| var hostType = typeof(CopilotClient).Assembly.GetType("GitHub.Copilot.FfiRuntimeHost", throwOnError: true)!; | ||
| var constructor = hostType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic) | ||
| .Single(candidate => candidate.GetParameters().Length == 8); | ||
| using var host = (IDisposable)constructor.Invoke( | ||
| [ | ||
| "test-runtime", | ||
| null, | ||
| null, | ||
| Array.Empty<string>(), | ||
| NullLogger.Instance, | ||
| new Func<uint, bool>(_ => | ||
| { | ||
| Interlocked.Increment(ref closeCalls); | ||
| return true; | ||
| }), | ||
| new Func<uint, bool>(_ => | ||
| { | ||
| Interlocked.Increment(ref shutdownCalls); | ||
| return false; | ||
| }), | ||
| new Action(() => Interlocked.Increment(ref callbackReleaseCalls)), | ||
| ]); | ||
|
|
||
| hostType.GetField("_connectionId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(host, (uint)21); | ||
| hostType.GetField("_serverId", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
| .SetValue(host, (uint)11); | ||
|
|
||
| host.Dispose(); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| Thread.Sleep(250); | ||
|
|
||
| Assert.Equal(1, Volatile.Read(ref closeCalls)); | ||
| Assert.Equal(1, Volatile.Read(ref callbackReleaseCalls)); | ||
| Assert.Equal(1, Volatile.Read(ref shutdownCalls)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.