Skip to content

Make supervisor split-line stdout test actually cover the split in Task SDK - #72585

Closed
ColtenOuO wants to merge 1 commit into
apache:mainfrom
ColtenOuO:fix-supervisor-pipe-split-write-test
Closed

ColtenOuO wants to merge 1 commit into
apache:mainfrom
ColtenOuO:fix-supervisor-pipe-split-write-test

Conversation

@ColtenOuO

Copy link
Copy Markdown
Contributor

Summary

TestWatchedSubprocess::test_reading_from_pipes exists to prove that the supervisor reassembles a log line that arrives across two separate writes. Its own comment admitted the mechanism was shaky:

# We need a short sleep for the main process to process things. I worry this timing will be
# fragile, but I can't think of a better way. This lets the stdout be read (partial line) and the
# stderr full line be read
sleep(0.1)

Investigating that fragility turned up something worse than a timing race: the test never exercised the split at all, and could not have failed if the partial-line handling broke.

The child process writes the first half of the line with sys.stdout.write("Message "). Inside the forked child, sys.stdout is an io.TextIOWrapper(binary, line_buffering=True) (see _reopen_std_io_handles in task-sdk/src/airflow/sdk/execution_time/supervisor.py). Line buffering only flushes on a newline, and "Message " has none — so that text sat in the child's own userspace buffer and never reached the socket. The parent had nothing partial to read during the sleep(0.1); the bytes were only handed over later, when the second write appended "split across two writes\n" and the newline triggered a single flush of the whole line.

Both paths produce exactly the same assertion target — one task.stdout record reading Message split across two writes — so the test passed either way. This was not a flaky failure but a silent loss of coverage: the partial-line branch of make_buffered_socket_reader could have regressed at any point and this test would have kept passing.

Test

By wrapping the parent's task.stdout handler and using recv(..., MSG_PEEK) to record the exact bytes the parent saw on each read, with and without an explicit flush in the child:

version bytes the parent actually read
current main b"I'm a short message\n" then b'Message split across two writes\n'
with flush() added b"I'm a short message\nMessage " then b'split across two writes\n'

The first row is the bug: the parent's second read already contains the complete line, terminating newline included. No partial line ever crossed the socket.

What was happening before

sequenceDiagram
    autonumber
    participant C as child code
    participant B as child stdout buffer<br/>TextIOWrapper, line buffered
    participant P as parent supervisor

    C->>B: print "I'm a short message"
    B->>P: flush on newline: I'm a short message + LF
    Note over P: first read emits one log record

    C->>B: write "Message "
    Note over B: no newline, so no flush<br/>the bytes never reach the socket

    Note over C,P: sleep 0.1s — parent sits in select with nothing to read

    C->>B: write "split across two writes" + LF
    B->>P: flush on newline: Message split across two writes + LF
    Note over P: second read already holds the complete line<br/>the partial-line branch is never taken
Loading

What happens now

sequenceDiagram
    autonumber
    participant C as child code
    participant B as child stdout buffer
    participant P as parent supervisor

    C->>B: print "I'm a short message"
    B->>P: flush on newline: I'm a short message + LF
    P-->>C: set stdout_consumed after first read
    Note over C: wait for stdout_consumed, then clear it

    C->>B: write "Message "
    C->>B: flush
    B->>P: Message
    Note over P: second read has no newline<br/>the partial line is held in the reader buffer
    P-->>C: set stdout_consumed after second read
    Note over C: wait for stdout_consumed

    C->>B: write "split across two writes" + LF
    B->>P: split across two writes + LF
    Note over P: third read completes the buffered line<br/>and emits one log record
Loading

The guarantee is structural rather than probabilistic. When the second wait returns, the only thing the parent can have consumed is the partial line, because the first wait already confirmed the short message was drained and then cleared the event. If the parent were to somehow not read the partial line, the child raises after a 10 second timeout and exits non-zero, so assert rc == 0 fails the test — the old "quietly skipped the interesting case" outcome is no longer reachable.

The reworked test was run 20 times in a row, passing 20/20, with no sleep left in it.

Change

  • task-sdk/tests/task_sdk/execution_time/test_supervisor.py
    • The child flushes after writing the partial line, so it actually reaches the parent.
    • sleep(0.1) is replaced by a two-step handshake: the test wraps ActivitySubprocess._create_log_forwarder for the task.stdout channel only and signals a multiprocessing.Event after each read; the child waits for the short message to be consumed, writes and flushes the partial line, waits for that to be consumed, and only then writes the remainder.

Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 5)

The test that is supposed to exercise the parent reading a partial line
never did. The child's stdout is line buffered, so writing "Message "
without a newline left the text in the child's own buffer instead of
sending it down the socket; the parent only ever saw the complete line
once the second write flushed it. The sleep the test relied on to make
the parent read a partial line had nothing to read during it, and both
outcomes produce the same log record, so the missing coverage could
never show up as a failure.

The child now flushes the partial line and waits for the parent to
confirm it has consumed it before writing the rest, so the partial-line
path is guaranteed to be taken and a regression there fails the test
instead of passing quietly.
@potiuk

potiuk commented Sep 25, 2026

Copy link
Copy Markdown
Member

Hello @ColtenOuO - thank you for your contributions to Apache Airflow!

The Airflow community has introduced a limit of 5 open pull requests at a time for contributors without write access to the repository. You currently have 19 open pull requests, so - as a one-time step of introducing the limit - we closed the ones where maintainers have not engaged yet:

These pull requests stay open because maintainers are already engaged in them - they count towards your limit:

This is not a judgement of you or of your changes. We never told contributors before that opening many pull requests at once was a problem, so there is nothing to feel bad about - and nothing is lost: your branches, commits and the review history stay where they are.

What we ask you to do is to make your first prioritization decision: choose which of the pull requests above matter most to you, and reopen them (up to 5 open at a time, including the ones still open) with the "Reopen pull request" button or gh pr reopen <PR_NUMBER> --repo apache/airflow. Reopen the ones you are ready to follow through - keep them rebased, respond to review comments and fix failing checks.

While your pull requests are waiting for review, the most valuable thing you can do is help in other ways - reviewing other contributors' pull requests, helping with issues, and taking part in the discussions on the devlist and Slack.

Why we introduced the limit, what it means for you and how to reopen or restore a pull request is explained in https://github.com/apache/airflow/blob/main/contributing-docs/32_open_pull_request_limit.rst.


Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:task-sdk closed because of open PR limit Closed as a one-time step of introducing the open pull request limit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants