Skip to content

Add shutDown method to PluginMessageHandler #2915

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 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftCompilerPlugin/CompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ extension CompilerPlugin {
let connection = try StandardIOMessageConnection()
let provider = MacroProviderAdapter(plugin: Self())
let impl = CompilerPluginMessageListener(connection: connection, provider: provider)
impl.main()
try impl.main()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ public class CompilerPluginMessageListener<Connection: MessageConnection, Handle
///
/// On internal errors, such as I/O errors or JSON serialization errors, print
/// an error message and `exit(1)`
public func main() {
public func main() throws {
#if os(WASI)
// Rather than blocking on read(), let the host tell us when there's data.
readabilityHandler = { _ = self.handleNextMessage() }
#else
while handleNextMessage() {}
try self.handler.shutDown()
#endif
}

Expand Down Expand Up @@ -133,6 +134,12 @@ public class CompilerPluginMessageListener<Connection: MessageConnection, Handle
public protocol PluginMessageHandler {
/// Handles a single message received from the plugin host.
func handleMessage(_ message: HostToPluginMessage) -> PluginToHostMessage

/// Deterministically and synchronously cleans up resources that cannot be dealt with in
/// a deinitializer due to possible errors thrown during the clean up. Usually this
/// includes closure of file handles, sockets, shutting down external processes and IPC
/// resources set up for these processes, etc.
func shutDown() throws
}

/// A `PluginMessageHandler` that uses a `PluginProvider`.
Expand Down Expand Up @@ -226,6 +233,10 @@ public class PluginProviderMessageHandler<Provider: PluginProvider>: PluginMessa
return .loadPluginLibraryResult(loaded: diags.isEmpty, diagnostics: diags)
}
}

/// Empty implementation for the default message handler, since all resources are automatically
/// cleaned up in the synthesized initializer.
public func shutDown() throws {}
}

@_spi(PluginMessage)
Expand Down