Skip to content

Do not crash when a Python TextParserStreamer has no write() override - #4549

Merged
Wovchena merged 5 commits into
openvinotoolkit:masterfrom
goodmai:text-parser-streamer-no-override
Oct 2, 2026
Merged

Wovchena merged 5 commits into
openvinotoolkit:masterfrom
goodmai:text-parser-streamer-no-override

Conversation

@goodmai

@goodmai goodmai commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

ConstructableTextParserStreamer::write(JsonContainer&) called the Python write override without checking that there is one. A TextParserStreamer constructed directly in Python, with no subclass, then crashed the interpreter (SIGSEGV) on the first chunk. That happens in the streaming thread of LLMPipeline and VLMPipeline, or on _write.

Following review, a missing override is now an error, raised on the calling thread. Raising from write() itself would not do, because it runs on the streamer's worker thread, where an exception terminates the process.

  • Constructing TextParserStreamer itself raises TypeError, since write() is pure virtual. This uses a pybind11 dual py::init factory: the base class factory throws, and Python subclasses construct as before.
  • A subclass that does not implement write() raises TypeError when it is called, instead of calling a null override.

Fixes #4548

Tests

  • New test_text_parser_streamer_without_write_override in tests/python_tests/test_parsers.py. It checks that constructing TextParserStreamer directly raises TypeError (on master this constructs, then segfaults on the first chunk), and that a subclass without write() raises TypeError when called.
  • test_parsers.py -m "not nightly": 25 passed after rebasing on master. Stubs regenerated with pybind11-stubgen 2.5.5 are unchanged.
  • Manual run: a plain TextParserStreamer passed to VLMPipeline.generate() with a real Qwen3.5-family model on an Intel Arc GPU. Master crashes. This was the first version of this PR; with the current version, construction raises TypeError instead.

Found while testing #4547.

AI assistance used: yes
If yes: Claude (an AI coding assistant) debugged the crash with gdb, drafted the fix and the test, and ran the checks under my direction.
Human validation performed: local build (OpenVINO 2026.5 nightly, GCC 16, -DCMAKE_CXX_STANDARD=17, see #4531), the parser tests above with the new test failing before and passing after, and the manual VLMPipeline run.

Checklist:

  • This PR follows GenAI Contributing guidelines.
  • Tests have been updated or added to cover the new code: tests/python_tests/test_parsers.py::test_text_parser_streamer_without_write_override.
  • This PR fully addresses the ticket.
  • N/A: I have made corresponding changes to the documentation.

🤖 Generated with Claude Code

Comment thread src/python/py_streamers.cpp Resolved
@Wovchena
Wovchena requested review from apaniukov and pavel-esir and removed request for as-suvorov September 28, 2026 15:21
goodmai and others added 2 commits September 29, 2026 09:33
TextParserStreamer is documented as a base class, but it can be constructed
and passed to generate() as is. Its C++ write(JsonContainer&) then called the
missing Python override through a null handle and segfaulted in the streaming
thread. Without an override it now only collects the parsed message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Constructing TextParserStreamer itself now raises TypeError, since write() is
pure virtual. A subclass that does not implement write() raises TypeError when
it is called, instead of the silent no-op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@goodmai
goodmai force-pushed the text-parser-streamer-no-override branch from a05052f to 2983e4c Compare September 29, 2026 07:52
@Wovchena
Wovchena requested review from Wovchena and a balanced review from Copilot September 29, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The new exception can still escape the streaming worker thread and terminate the process.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Prevents unsafe direct construction of Python TextParserStreamer and reports missing write() overrides.

Changes:

  • Adds subclass-aware pybind11 construction.
  • Adds override validation and regression tests.
File Description
src/​python/​py_streamers.cpp Adds constructor and override checks.
tests/​python_tests/​test_parsers.py Tests missing write() behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/python/py_streamers.cpp Resolved
…the caller

An exception thrown by StreamerBase::write() on the worker thread had no handler
and terminated the process, e.g. for a TextParserStreamer subclass without write()
passed to a continuous batching pipeline. Catch it in the worker, stop streaming
and rethrow it from end() on the calling thread. The destructor swallows it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@goodmai

goodmai commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

@Wovchena, since your approval I pushed 6fd2d01, which addresses the Copilot finding (write() runs in ThreadedStreamerWrapper::_worker, where an exception called std::terminate). The worker now catches it and end() rethrows it on the calling thread; there is a new generation-path test. Details: #4548 (comment)

Could you take another look at the threaded_streamer.hpp change? I cannot re-request the review myself (no write access). cc @pavel-esir @apaniukov

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Error finalization can invoke the failing callback twice, duplicating side effects and masking the original exception.

Review effort: Balanced
Findings: 2 High severity

Open (2)

@Wovchena Wovchena left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In addition to Copilot's comment

ThreadedStreamerWrapper's destructor logs the error it swallows with GENAI_ERR().
end() is idempotent and, when write() failed on the worker thread, rethrows that
error without calling StreamerBase::end(): TextStreamer::end() would flush the
cached text through the failed write() again and could replace the original error.
@goodmai

goodmai commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

@Wovchena both points are addressed in a2a8bb6: GENAI_ERR() in the destructor, and the Copilot finding (no StreamerBase::end() after a failed write(), end() is idempotent, the test now checks write() runs once). Could you take another look?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The crash and worker-thread exception paths are handled with focused regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (2)

@Wovchena

Wovchena commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

build_jenkins

@Wovchena
Wovchena enabled auto-merge October 2, 2026 08:23
@Wovchena
Wovchena added this pull request to the merge queue Oct 2, 2026
Merged via the queue into openvinotoolkit:master with commit ff212f9 Oct 2, 2026
288 of 290 checks passed
@sgonorov sgonorov mentioned this pull request Oct 25, 2026
1 of 4 tasks
Stanley00 pushed a commit to stanley-fork/openvino.genai that referenced this pull request Oct 30, 2026
## Description

Reworks `.github/copilot-instructions.md` so that Copilot code review
gives fewer noisy comments and catches the issues human reviewers keep
raising. Before this change, the file mixed persona text, vague style
rules and rules that Copilot can't check from a diff. The review section
also sat at the end of the file, past the 4,000-character limit that
code review used until June 2026.

The rules were checked against Copilot's comments on the last 100 PRs:
about 610 inline comments, of which authors accepted about 45%. The
noise patterns found there are removed or scoped below.

**Removed**
- Persona and "Expertise Areas" block: it gives the reviewer nothing to
check.
- "Follow C++ Core Guidelines strictly" and "Include C++ Core Guidelines
references in review comments": too broad to act on, and the model tends
to cite wrong rule numbers. GitHub also lists comment-format
instructions as unsupported.
- "Descriptive names" and "avoid pronouns": vague, so they lead to
wording nitpicks.

**Fixed**
- "Pass by const ref wherever possible" clashed with taking moved-from
arguments as `&&`. The rule now says: `ov::Tensor` and other non-trivial
objects by `const&`, moved-from arguments as `&&`. This matches the
codebase (693 `const ov::Tensor&` parameters, 131 by value) and review
practice (openvinotoolkit#4501, openvinotoolkit#4573).
- "ABI stability isn't required" is now an action: don't flag ABI
breaks, but flag public API that is removed, renamed or loses a default
argument (openvinotoolkit#4541).
- "Reject GPU tests" is merged with a rule about tests gated on env vars
or models that CI never enables. Both kinds never run.
- Rules are grouped by topic, numbered without restarts, and use
imperative wording.

**Added**, from repeated reviewer comments on recent PRs:
- C++/Python API parity, including the C and JS bindings (openvinotoolkit#4247). Tests
already enforce the `.pyi` stubs, so review doesn't comment on them.
- An output tensor from `get_tensor()` is a view into the request
buffer, so one kept past the next `infer()` needs a deep copy of its
data. Reusing input tensors is fine.
- Don't drop existing error paths (openvinotoolkit#4356, openvinotoolkit#4549). Reuse existing helpers
(openvinotoolkit#4241, openvinotoolkit#3881, openvinotoolkit#4338). Keep the public API small (openvinotoolkit#4310). Use
model-neutral public names. Don't use deprecated APIs in new code.
Comment every `const_cast`.
- Tests: exact equality for repeated and greedy CPU runs, a test that
shows each new option's effect, `parametrize` instead of
near-duplicates, a ticket for every skip/xfail, cheap tests (openvinotoolkit#4247,
openvinotoolkit#4592).
- CI wiring: new test files in the matching `test_matrices` file for
each OS, `-m` filters that match markers, exact pins, and
labeler/components/CODEOWNERS entries when a new use case is introduced
(openvinotoolkit#4247, openvinotoolkit#4575, openvinotoolkit#4090). `!=` instead of a new upper cap when a release
breaks something (openvinotoolkit#2858).
- One purpose per PR (openvinotoolkit#4310). Comment hygiene and permalinks to commits
(openvinotoolkit#4452). Copyright header in new files. Complete removals. C++ and
Python samples and docs updated together.

**Scoped to avoid false positives**
- API rules apply only to the public API in `src/cpp/include` and the
bindings. The unused-function and unused-return-value rules apply only
to non-public code.
- Error-message rules apply only to checks on user input.
- No test requests for documentation, CI, sample-only or xfail-only
changes, or for test utilities and tokenizer behavior (openvinotoolkit#4592). Tests
marked `real_models` or `nightly` are not flagged as "never run in CI".
A missing `tiny-random` model may be a ticketed follow-up (openvinotoolkit#4591).
- Exempt: override parameters, performance options, catch-all blocks at
the C and JS binding boundaries, the `device` argument and sibling flags
in samples, and the dependabot-managed `<=` caps in `tools/`, `samples/`
and the root.
- Samples are minimal demos, so no requests for edge-case handling there
(openvinotoolkit#4670).

Planned follow-ups: openvinotoolkit#4716 splits the rules into path-scoped files and
adds the rules that need discussion.

**Copilot configuration PRs:**
1. openvinotoolkit#4714 Rework Copilot review instructions. Merge first.
2. openvinotoolkit#4716 Split Copilot rules into path-scoped files. Stacked on openvinotoolkit#4714;
merge after it.
3. openvinotoolkit#4715 Fix Copilot agent and skill metadata. Independent; merge in any
order.

## Checklist:
- [x] This PR follows [GenAI Contributing
guidelines](https://github.com/openvinotoolkit/openvino.genai?tab=contributing-ov-file#contributing).
- [ ] N/A Tests have been updated or added to cover the new code.
- [ ] N/A This PR fully addresses the ticket.
- [ ] N/A I have made corresponding changes to the documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: TextParserStreamer without a Python write() override segfaults

3 participants