Improve thread pool error handling#273
Merged
DeRauk merged 6 commits intocoinbase:masterfrom Oct 30, 2023
Merged
Conversation
jeffschoner
commented
Oct 22, 2023
|
|
||
| describe Temporal::Activity::Context do | ||
| let(:client) { instance_double('Temporal::Client::GRPCClient') } | ||
| let(:connection) { instance_double('Temporal::Connection::GRPC') } |
Contributor
Author
There was a problem hiding this comment.
This turned out to be the wrong/a non-existent type. It's not strictly necessary for this PR, but I fixed it while I was in here.
Comment on lines
-102
to
+104
| subject.heartbeat(iteration: 3) | ||
| expect(subject.last_heartbeat_throttled).to be(true) | ||
|
|
||
| # Shutdown to drain remaining threads | ||
| heartbeat_thread_pool.shutdown |
Contributor
Author
There was a problem hiding this comment.
This code was raising some sort of error that was previously being ignored. Now that unhandled errors result in crashes to the process, this needs to be properly shutdown so that threads encountering bad test state don't fail.
Contributor
Author
|
This change has also been running within Stripe since around July with good results |
DeRauk
approved these changes
Oct 30, 2023
Contributor
|
Thanks for this PR @jeffschoner. The example silent failure you gave in the description sounds nasty and tough to debug. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
All errors and exceptions coming out of a thread pool job are now logged and sent to the error handler
If any error does reach the top of the stack on a thread pool thread, it will now crash the process rather than silently kill the thread. Because
StandardErroris rescued in theTaskProcessor, this impacts only severe errors likeNoMemoryErrororSecurityErrorraised by activity or workflow code, or ordinary error raised due to bugs in temporal-ruby itself.It's perhaps somewhat controversial to crash the worker process, but
Exceptionraised out of user code or unexpected errors in the pollers, task processors, or thread pools, leave the worker in an unknown state, where it's unclear that it can continue to safely process work.Motivation
Before this change, when an activity or workflow task raises an error that is not a subclass of
StandardError, it silently kills the thread pool thread it is running on. Additionally, any errors in the ensure/rescue blocks in the pollers, task processors or the thread pool will cause this same behavior. Eventually, workers can run out of thread pool threads and become "zombie" workers that will stop polling for tasks, all while logging no errors.For example, I've seen this occur when an error raised by an activity contains a circular reference. When Oj tries to serialize it, it will run out of memory and raise
NoMemoryError. This is a subclass ofExceptionnotStandardError, and therefore is not caught by an ordinaryrescue => eclause. Eventually this reaches the top of the activity task thread pool thread and causes it to silently exit before it can increment theavailable_threadsand signal theavailabilitycondition variable to free up resources. Memory frees up at this point, so the worker continues to run, but other failures may have occurred on other threads in the process during this period of memory exhaustion, leaving the worker in an unknown state. The activity will then be retried after it times out, which in turn kills another thread on one of the workers. If this happens enough times between worker service restarts, the entire worker fleet will not be able to process any activities.Testing
There are new specs for these thread pool cases in both regular and scheduled thread pools. Some other specs also had to be updated because they had errors throwing on a background thread that started surfacing hidden failures.