Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve robustness of subprocess text streaming #6445

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Aug 17, 2022

Summary

Previously, this utility would call blocking writes to stderr and stdout directly. This appears to introduce the possibility of race conditions and blocked event loops when many processes are run concurrently. Since we use these utilities to launch flows in parallel processes from the agent, it is important that they are robust to concurrency. wrap_file may not be thread safe, but this is still an improvement from where we were. There is not a clear suggested pattern for this, see extensive discussion at python-trio/trio#174.

Attempts to address #6335

Steps Taken to QA Changes

I wrote a recursive subprocess test

from prefect.utilities.processutils import run_process
import anyio
import sys


async def main(count):
    if count <= 0:
        return

    print(f"Running {count} children")

    async with anyio.create_task_group() as tg:
        for _ in range(count):
            await tg.start(
                run_process, [sys.executable, "./process-test.py", str(count - 2)], True
            )

if __name__ == "__main__":
    anyio.run(main, int(sys.argv[1]))

Checklist

This pull request is:

  • A documentation / typographical error fix
    • No tests or issue needed
  • A short code fix
    • Please reference the related issue by including "closes <link to issue>" in this Pull Request's summary section.
      • If no issue exists, please create a bug report issue
    • Please include tests. One-line fixes without tests will not be accepted unless it's related to the documentation only.
  • A new feature implementation
    • Please reference the related issue by including "closes <link to issue>" in this Pull Request's summary section.
      • If no issue exists, please create a feature enhancement issue
    • Please include tests
    • Please make sure that your QA steps are both thorough and easy to reproduce by somebody with limited knowledge of the feature that you are submitting

Happy engineering!

Previously, this utility would call blocking writes to stderr and stdout directly. This appears to introduce the possibility of race conditions and blocked event loops when many processes are run concurrently. Since we use these utilities to launch flows in parallel processes from the agent, it is important that they are robust to concurrency. `wrap_file` _may_ not be thread safe, but this is still an improvement from where we were. There is not a clear suggested pattern for this, see extensive discussion at python-trio/trio#174.
@zanieb zanieb added the bug Something isn't working label Aug 17, 2022
Copy link
Collaborator

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

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

Excellent find!

Copy link
Contributor

@peytonrunyan peytonrunyan left a comment

Choose a reason for hiding this comment

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

lgtm!

@zanieb zanieb merged commit 5a22374 into main Aug 17, 2022
@zanieb zanieb deleted the improve-subprocess-streaming branch August 17, 2022 18:30
@zanieb zanieb added fix A fix for a bug in an existing feature and removed bug Something isn't working labels Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A fix for a bug in an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants