Skip to content

Conversation

@rajanyadav0307
Copy link
Contributor

Issue #, if available:
N/A

Description of changes:
This PR improves the thread-safety and lifetime correctness of ThreadTask.

Changes include:

  • Reordering data members so std::thread is destroyed last, ensuring all
    members accessed by the worker thread remain valid during destruction.
  • Promoting m_detached to std::atomic<bool> to prevent data races when
    accessed concurrently by multiple threads.
  • Updating the constructor initializer list to match the member declaration
    order and using a lambda for thread startup for improved clarity.

These changes do not modify the public API and are purely internal
correctness and safety improvements.

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (Existing threading behavior is exercised; no new tests required.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and
redistribute this contribution, under the terms of your choice.

Change:
Reorder ThreadTask data members so std::thread is destroyed last, ensuring
all members accessed by the running thread remain valid during shutdown.
Also make the detach flag atomic to avoid potential data races between the
executor thread and the owning thread.

Reason:
This change improves thread-safety and prevents subtle undefined behavior
without modifying the public API.
@rajanyadav0307
Copy link
Contributor Author

@sbiscigl ,

Please can you review the changes?

Thanks

@sbiscigl
Copy link
Contributor

sbiscigl commented Jan 13, 2026

Added proper tests to cover this PR. (Existing threading behavior is exercised; no new tests required.)

So while this is true, can you write any tests or CI checks that would fail before your change that would show improved correctness and saftey?

@rajanyadav0307
Copy link
Contributor Author

Thanks @sbiscigl ,

Good question. ThreadTask isn’t referenced directly in tests, but it’s used internally by PooledThreadExecutor, so executor tests already exercise it indirectly.

My plan is to add a TSAN-gated stress test to PooledThreadExecutorTest.cpp that races executor shutdown with concurrent Submit() calls to hit the detach/shutdown path. This should surface the existing race on m_detached under ThreadSanitizer and show the benefit of making it atomic. I’ll follow up once that’s in place.

The std::thread reordering is a small construction-order safety improvement to avoid partially constructed state being observed by the worker.

@sbiscigl
Copy link
Contributor

So in general when we accept PR's we want reproducible issues that are impacting customers. It is a slippery slope to change implementation of key components of the SDK without real proven impact.

So from a user standpoint how could you use the SDK in way where this issue would come up? i.e. how could invoking a client API trigger this behavior and can you trigger it in a reproducible way? That is the kind of test I would want to see along this change. One that i can verify that this change is fixing something for users.

@rajanyadav0307
Copy link
Contributor Author

@sbiscigl
It's true changing code that is in production without any evidence can bring impacts. Will try to see if I could design any test case around something you described. If I get any, will update it.

Thanks.

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