Skip to content

Conversation

@MaxDesiatov
Copy link
Contributor

Motivation:

These signal handlers are useful to other tools that don't have access to SwiftTool. For example, DestinationCommand would benefit from this as it needs to set up its own cancellator for handling archiver processes.

Modifications:

Moved signal handler setup to a new installSignalHandlers function on Cancellator. Made captures of Cancellator in handler closures weak so that it's not referenced indefinitely and is released when no longer needed.

Result:

Cancellator/installSignalHandler can be used in more places.

These signal handlers are useful to other tools that don't have access to `SwiftTool`. For example, `DestinationCommand` would benefit from this as it needs to set up its own cancellator for handling archiver processes.
@MaxDesiatov MaxDesiatov requested a review from compnerd April 5, 2023 22:24
@MaxDesiatov MaxDesiatov self-assigned this Apr 5, 2023
@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@tomerd
Copy link
Contributor

tomerd commented Apr 5, 2023

no sure how I feel about this moving to the cancellator. maybe a new static utility?

@tomerd
Copy link
Contributor

tomerd commented Apr 5, 2023

if we are moving to a utility like this we should also add precondition to make sure it is not called more than once

@MaxDesiatov
Copy link
Contributor Author

if we are moving to a utility like this we should also add precondition to make sure it is not called more than once

I've added a check for this via private static var on Cancellator on the latest commit.

no sure how I feel about this moving to the cancellator. maybe a new static utility?

Such utility would still need to take a instance of a cancellator as an argument, and then is there much difference between taking it as a function argument, or via self? The latter seems more obvious at the point of use IMO.

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

interruptSignalSource.resume()
#endif

Self.isSignalHandlerInstalled = true
Copy link
Contributor

Choose a reason for hiding this comment

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

non blocking but this is not thread safe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a lock for the whole function body to make sure that there's no race condition when installing these handlers.

internal init(
outputStream: OutputByteStream,
options: GlobalOptions,
shouldInstallSignalHandlers: Bool = true,
Copy link
Contributor

Choose a reason for hiding this comment

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

we can consider adding this to global options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GlobalOptions is dedicated to properties using @Option property wrappers passed from ArgumentParser. Would this ToolWorkspaceConfiguration type values of which are passed as arguments to this initializer also work?

return try SwiftTool(
outputStream: outputStream,
options: options,
shouldInstallSignalHandlers: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

feels like this belongs into options

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to ToolWorkspaceConfiguration for now per my comment above.

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci test windows

@MaxDesiatov
Copy link
Contributor Author

@swift-ci smoke test

@MaxDesiatov MaxDesiatov merged commit 5de521d into main Apr 8, 2023
@MaxDesiatov MaxDesiatov deleted the maxd/cancellator-signal-handlers branch April 8, 2023 08:12
MaxDesiatov added a commit that referenced this pull request Apr 8, 2023
These signal handlers are useful to other tools that don't have access to `SwiftTool`. For example, `DestinationCommand` would benefit from this as it needs to set up its own cancellator for handling archiver processes.

Moved signal handler setup to a new `installSignalHandlers` function on `Cancellator`. Made captures of `Cancellator` in handler closures weak so that it's not referenced indefinitely and is released when no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants