fix: use local StringIO reference to prevent stderr race condition in IOHandler._render#6080
Conversation
When multiple workflows execute concurrently, the global sys.stderr substitution in IOHandler._render causes a race condition. One thread can restore sys.stderr to the real TextIOWrapper before another thread calls .getvalue() on it, resulting in: '_io.TextIOWrapper' object has no attribute 'getvalue' Fix by keeping a local reference to the StringIO buffer and wrapping the operation in try/finally to ensure sys.stderr is always restored. Fixes keephq#6079
|
@Aladex is attempting to deploy a commit to the KeepHQ Team on Vercel. A member of the Team first needs to authorize it. |
Adds a test that reproduces the race condition where multiple threads calling render() simultaneously would cause AttributeError on sys.stderr. The test uses a monkey-patched delay in render_recursively to force thread interleaving, making the race deterministic. Ref: keephq#6079
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6080 +/- ##
===========================================
- Coverage 46.39% 30.50% -15.90%
===========================================
Files 176 101 -75
Lines 18412 11685 -6727
===========================================
- Hits 8543 3564 -4979
+ Misses 9869 8121 -1748 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
💪 Fantastic work @Aladex! Your very first PR to keep has been merged! 🎉🥳 You've just taken your first step into open-source, and we couldn't be happier to have you onboard. 🙌 For any support, feel free to reach out on the community: https://slack.keephq.dev. Happy coding! 👩💻👨💻 |
… IOHandler._render (keephq#6080) Co-authored-by: Shahar Glazner <[email protected]>
Summary
IOHandler._renderwhere concurrent workflow executions cause'_io.TextIOWrapper' object has no attribute 'getvalue'StringIObuffer instead of reading fromsys.stderrwhich may have been restored by another threadtry/finallyto ensuresys.stderris always restored even if rendering raisesFixes #6079