Skip to content

Address some race conditions during the graceful shutdown process. #166

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

Conversation

tachyonics
Copy link

Motivation:

Address and add tests for the following race conditions-

  1. If .signalSequenceFinished, .gracefulShutdownCaught or .gracefulShutdownFinished occur during graceful shutdown, gracefulShutdownIndex will be incremented without a service being shutdown causing the process to think the wrong service shutdown signal has been received next, failing shutdown incorrectly.
  2. If a signal is caught that isn't a cancellationSignal, the same thing will happen as (1).
  3. If a service throws out of order (the serviceGroup is waiting for ServiceB to gracefully shutdown but ServiceA throws an error), gracefulShutdownIndex will be incremented without waiting for the 'current' service to shutdown. This may incorrectly fail the shutdown process for some failureTerminationBehavior modes.
  4. The shutdownGracefully function cancels the group which may cause the cancellationCaught case in the _run loop to be triggered next. It is unneccesary to call cancelGroupAndSpawnTimeoutIfNeeded here in this case.

Modifications:

  1. Modified the task group result loop in shutdownGracefully to only increment the service iterator when the service being waited on has been confirmed as shut down.
  2. Track out-of-order service shutdowns using a mutable runningServices array.
  3. Add debug logs to signals that were previously silently consumed.
  4. Add a mechanism for injecting task group results for testing (if there is a better mechanism, let me know).
  5. Added unit tests to verify the race conditions addressed.

Result:

As verified by the added unit tests, the race conditions will be handled as expected.

Motivation:

Address and add tests for the following race conditions-
1. If `.signalSequenceFinished`, `.gracefulShutdownCaught` or `.gracefulShutdownFinished` occur during graceful shutdown,
   `gracefulShutdownIndex` will be incremented without a service being shutdown causing the process to think the wrong
   service shutdown signal has been received next, failing shutdown incorrectly.
2. If a signal is caught that isn't a `cancellationSignal`, the same thing will happen as (1).
3. If a service throws out of order (the serviceGroup is waiting for ServiceB to gracefully shutdown but ServiceA throws an error),
   `gracefulShutdownIndex` will be incremented without waiting for the 'current' service to shutdown. This may incorrectly fail the shutdown
   process for some `failureTerminationBehavior` modes.
4. The `shutdownGracefully` function cancels the group which may cause the `cancellationCaught` case in the `_run` loop
   to be triggered next. It is unneccesary to call `cancelGroupAndSpawnTimeoutIfNeeded` here in this case.

Modifications:

1. Modified the task group result loop in `shutdownGracefully` to only increment the service iterator when the service being waited on has been
confirmed as shut down.
2. Track out-of-order service shutdowns using a mutable `runningServices` array.
3. Add debug logs to signals that were previously silently consumed.
4. Add a mechanism for injecting task group results for testing (if there is a better mechanism, let me know).
5. Added unit tests to verify the race conditions addressed.

Result:

As verified by the added unit tests, the race conditions will be handled as expected.
FranzBusch added a commit that referenced this pull request Jan 26, 2024
# Motivation

This should fix the remaining issues raised in #166. The problem here was that if a service finished/threw out of order then we were wrongly treating this as if the service that we are currently shutting down finished.

# Modification
This PR ensures that we use the same `services` array during the graceful shutdown to nil out services that have finished. This way we correctly keep track of any service that finished. Additionally, there was a separate bug where we started to shutdown the next service to early if another service threw and had the termination behaviour of `shutdownGracefully`.

# Result
No more incorrect shutdown orderings.
FranzBusch added a commit that referenced this pull request Jan 26, 2024
# Motivation

This should fix the remaining issues raised in #166. The problem here was that if a service finished/threw out of order then we were wrongly treating this as if the service that we are currently shutting down finished.

# Modification
This PR ensures that we use the same `services` array during the graceful shutdown to nil out services that have finished. This way we correctly keep track of any service that finished. Additionally, there was a separate bug where we started to shutdown the next service to early if another service threw and had the termination behaviour of `shutdownGracefully`.

# Result
No more incorrect shutdown orderings.
@FranzBusch
Copy link
Contributor

I landed both #171 and #170. Those two should fix all the issues that you have identified here from what I can see. I also brought over your unit tests that weren't injecting a stream since I want to avoid introducing test code into the implementation.

FranzBusch added a commit that referenced this pull request Jan 26, 2024
# Motivation

This should fix the remaining issues raised in #166. The problem here was that if a service finished/threw out of order then we were wrongly treating this as if the service that we are currently shutting down finished.

# Modification
This PR ensures that we use the same `services` array during the graceful shutdown to nil out services that have finished. This way we correctly keep track of any service that finished. Additionally, there was a separate bug where we started to shutdown the next service to early if another service threw and had the termination behaviour of `shutdownGracefully`.

# Result
No more incorrect shutdown orderings.
FranzBusch added a commit that referenced this pull request Jan 26, 2024
* Fix logic issues during graceful shutdown

# Motivation

This should fix the remaining issues raised in #166. The problem here was that if a service finished/threw out of order then we were wrongly treating this as if the service that we are currently shutting down finished.

# Modification
This PR ensures that we use the same `services` array during the graceful shutdown to nil out services that have finished. This way we correctly keep track of any service that finished. Additionally, there was a separate bug where we started to shutdown the next service to early if another service threw and had the termination behaviour of `shutdownGracefully`.

# Result
No more incorrect shutdown orderings.

* Fix one more race condition
@FranzBusch
Copy link
Contributor

@tachyonics closing this since I think all the problems that you listed here have been fixed on the latest release. Please feel free to reopen if you think they are not!

@FranzBusch FranzBusch closed this Feb 12, 2024
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.

2 participants