Skip to content

Stop Vminitd.close() from trapping when it races runConnections() - #1014

Open
mvanhorn wants to merge 2 commits into
apple:mainfrom
mvanhorn:fix/970-vminitd-close-sigtrap
Open

mvanhorn wants to merge 2 commits into
apple:mainfrom
mvanhorn:fix/970-vminitd-close-sigtrap

Conversation

@mvanhorn

Copy link
Copy Markdown

Motivation

Closing the guest agent before its connection has started aborts the host process with SIGTRAP (exit 133). That happens when a caller cancels an in-flight container request, such as a statistics call, a few milliseconds after it begins. The caller gets a fatal error that an async writer was destroyed without finish(), instead of a cancellation or RPC error.

close() called beginGracefulShutdown() and then awaited the connection task. If that runs before the transport has consumed the preconfigured channel, the client moves from not-started to stopped without notifying the transport, runConnections() throws clientIsStopped, and connect() never runs. The outbound writer is finished only inside connect(), so the last release hits the writer’s deinit precondition. The same unfinished writer is left behind when the client is already running but the wrapped channel is still idle or configuring.

Summary

close() now cancels connectionTask and waits on connectionTask.result, ignoring the task’s error, instead of beginGracefulShutdown() followed by connectionTask.value. The connection task still enters the transport, so connect() runs and the preconfigured writer is finished. RPCs still in flight on this client are cancelled rather than drained. VminitdCloseTests drives an unread socket pair through forty immediate closes, one close after several yields, and a second close on the same value.

How it was tested

The targeted tests for the containerization Vminitd: change pass locally. The tests in Tests/ContainerizationTests/VminitdCloseTests.swift fail without the fix in the containerization Vminitd: change (checked by reverting the source changes). The new test Tests/ContainerizationTests/VminitdCloseTests.swift did not run here: no test runner for it was available.

Fixes #970

AI was used for assistance.

close() now cancels connectionTask and waits on connectionTask.result,
ignoring the task’s error, instead of beginGracefulShutdown() followed
by connectionTask.value. The connection task still enters the transport,
so connect() runs and the preconfigured writer is finished. RPCs still
in flight on this client are cancelled rather than drained.
VminitdCloseTests drives an unread socket pair through forty immediate
closes, one close after several yields, and a second close on the same
value.

Fixes apple#970
@@ -80,8 +80,10 @@ public struct Vminitd: Sendable {

/// Close the connection to the guest agent.
public func close() async throws {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you leave comment above it cancels in-flight RPCs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added in 03f6610: the doc comment on close() now says it cancels the connection task rather than shutting down gracefully, so any RPCs still in flight on the agent are cancelled too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants