Fix activity completion when thread interrupted flag is set
Fix the bug where activities that return with the 'interrupted' flag set, were unable to report their completion to the server. Activities can now successfully report completion even when they have the 'interrupted' flag set.
Without this fix, activity results are not successfully reported to server ( gRPC calls in ActivityWorker.sendReply() would fail with CancellationException when they have the 'interrupted' flag set). The fix clears the 'interrupted' flag before making gRPC calls in ActivityWorker.sendReply() and restores it afterward, ensuring:
- Activity results are successfully reported to the server
- Thread interruption semantics are preserved for worker shutdown
- All activity completion scenarios work (success/failure/cancellation)
Fixes: https://github.com/temporalio/sdk-java/issues/731 Related: https://github.com/temporalio/sdk-java/pull/722
What was changed
Fix the bug where gRPC calls fail when the thread's 'interrupted' flag is set, hence unable to report their completion status to the server.
Why?
The fix ensures that activities report their completion status to the Temporal server, while maintaining proper thread interruption semantics for the worker shutdown process.
According to the issue description and PR #722, the issue was that when an activity has 'interrupted' flag set:
i.e,
1 Catches an InterruptedException
2 Restores the 'interrupted' flag (Thread.currentThread().interrupt())
3 Continues to run and returns a result
The subsequent gRPC calls in ActivityWorker.sendReply() would fail with CancellationException ( gRPC request was
cancelled because the 'interrupted' flag was still set), hence activity result were not reported to the server.
The CancellationException is thrown in GrpcSyncRetryer when reporting activity completion results:
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CancellationException();
}
Solution
Modify ActivityWorker.sendReply() method to:
1 Check the 'interrupted' flag before making gRPC calls using Thread.interrupted() (which also clears the flag)
2 Temporarily clear the flag to allow gRPC calls to succeed
3 Make the gRPC call to report the activity result
4 Restore the 'interrupted' flag in a finally block if it was originally set
@Spikhalskiy
Checklist
-
Closes https://github.com/temporalio/sdk-java/issues/731
-
How was this tested:
1 Created and ran a comprehensive test that reproduces the exact scenario described in the bug report
2 Verified the test failed before the fix (confirming the bug existed)
3 Verified the test passes after the fix (confirming the bug is resolved)
4 Ran existing shutdown tests to ensure no regressions were introduced -
Any docs updates needed? No
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
Sojan Mathew seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.