Skip to content

fix: use local StringIO reference to prevent stderr race condition in IOHandler._render#6080

Merged
shahargl merged 3 commits into
keephq:mainfrom
Aladex:fix/iohandler-stderr-race-condition
Mar 9, 2026
Merged

fix: use local StringIO reference to prevent stderr race condition in IOHandler._render#6080
shahargl merged 3 commits into
keephq:mainfrom
Aladex:fix/iohandler-stderr-race-condition

Conversation

@Aladex
Copy link
Copy Markdown
Contributor

@Aladex Aladex commented Mar 9, 2026

Summary

  • Fix race condition in IOHandler._render where concurrent workflow executions cause '_io.TextIOWrapper' object has no attribute 'getvalue'
  • Keep a local reference to the StringIO buffer instead of reading from sys.stderr which may have been restored by another thread
  • Wrap in try/finally to ensure sys.stderr is always restored even if rendering raises

Fixes #6079

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
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 9, 2026

@Aladex is attempting to deploy a commit to the KeepHQ Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. Bug Something isn't working labels Mar 9, 2026
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
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 9, 2026
Copy link
Copy Markdown
Member

@shahargl shahargl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 9, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 9, 2026

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 30.50%. Comparing base (97c263a) to head (6321b36).
⚠️ Report is 52 commits behind head on main.

Files with missing lines Patch % Lines
keep/iohandler/iohandler.py 0.00% 7 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (97c263a) and HEAD (6321b36). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (97c263a) HEAD (6321b36)
2 1
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@shahargl shahargl merged commit 5890e8c into keephq:main Mar 9, 2026
12 of 14 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 9, 2026

💪 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. 🙌
If you're feeling adventurous, why not dive into another issue and keep contributing? The community would love to see more from you! 🚀

For any support, feel free to reach out on the community: https://slack.keephq.dev. Happy coding! 👩‍💻👨‍💻

wsimon1982 pushed a commit to wsimon1982/keep that referenced this pull request May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition in IOHandler._render: sys.stderr substitution is not thread-safe

2 participants