Skip to content

fix: don't shut down streaming servicers when the client closes the stream - #401

Open
grifftatarsky wants to merge 1 commit into
numaproj:mainfrom
grifftatarsky:fix/stream-close-generator-exit
Open

grifftatarsky wants to merge 1 commit into
numaproj:mainfrom
grifftatarsky:fix/stream-close-generator-exit

Conversation

@grifftatarsky

Copy link
Copy Markdown
Contributor

Fixes #400. Follow-up to #399.

The other async streaming servicers have the same bug as #398. When the client closes a response stream while the servicer is still yielding, gRPC calls aclose() on the generator and GeneratorExit is thrown at the yield. Each RPC's except BaseException records it as a UDF error and sets the shutdown event, so the UDF exits. This adds the same except GeneratorExit: return as the reducer (#399) and the reduce streamer (#331) to:

  • accumulator AccumulateFn
  • mapper MapFn
  • mapstreamer MapFn
  • batchmapper MapFn
  • sinker SinkFn
  • sourcetransformer SourceTransformFn
  • sourcer ReadFn and AckFn

The mapper and the source transformer weren't on the list in #399, but they have the same handler and fail the same way. The source's NackFn, PendingFn and PartitionsFn are unary, so they never see GeneratorExit. The sync servicers aren't changed here.

Tests:

  • One test_no_shutdown_when_*stream_closed per RPC in the existing *_shutdown.py files: read the first response, call aclose(), and check that neither the shutdown event nor _error is set. All 8 fail without the change.
  • tests/test_stream_close.py: the same over a real grpc.aio server and client for the mapper, mapstreamer, batchmapper, source transformer and ReadFn. The client cancels after one result while the rest are still queued. All 5 fail without the change. The sinker and AckFn send responses too small to back up, so a client cancel doesn't land on a yield there; the unit tests cover them and the accumulator.

make test passes on Python 3.11 and 3.14 (489 passed), black and ruff are clean, and tests/test_stream_close.py passed 30 runs in a row.

One thing I left alone: if the client cancels while the mapper, mapstreamer or source transformer is waiting for input, the CancelledError branch sets the shutdown event (with no error recorded). That branch is there for SIGTERM, and the servicer can't tell the two apart, so it seemed like a separate question.

AI disclosure: the reproduction, the change, the tests and this description were prepared with an AI coding agent (Claude Code).

…tream

When the client closes a response stream while the servicer is still
yielding, gRPC calls aclose() on the generator and GeneratorExit is
thrown at the yield. The except BaseException around each RPC treated
that as a UDF error and set the shutdown event. Return instead, as the
reducer and reduce streamer do, in the accumulator, mapper, mapstreamer,
batchmapper, sinker and source transformer, and in the source's ReadFn
and AckFn.

Signed-off-by: Griff <grifftatarsky@gmail.com>
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.20%. Comparing base (acc4a76) to head (3762872).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #401      +/-   ##
==========================================
+ Coverage   93.17%   93.20%   +0.02%     
==========================================
  Files          68       68              
  Lines        3694     3710      +16     
  Branches      236      236              
==========================================
+ Hits         3442     3458      +16     
  Misses        188      188              
  Partials       64       64              

☔ View full report in Codecov by Harness.
📢 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.

This branch has not been deployed

No deployments
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.

don't shut down the reducer when the client closes the stream

1 participant