Skip to content

Fix sink state when instances are reopened - #2459

Merged
Borda merged 2 commits into
roboflow:developfrom
Bortlesboat:agent/fix-sink-reuse-state
Jul 28, 2026
Merged

Fix sink state when instances are reopened#2459
Borda merged 2 commits into
roboflow:developfrom
Bortlesboat:agent/fix-sink-reuse-state

Conversation

@Bortlesboat

Copy link
Copy Markdown
Contributor
Before submitting
  • Self-reviewed the code
  • Updated the changelog
  • Added/updated tests
  • All tests pass locally

Description

Reset per-output state whenever an existing sv.CSVSink or sv.JSONSink instance successfully opens a new file.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Motivation and Context

Sink instances retain format-specific state after they close. Reopening a CSVSink leaves header_written and the prior field schema intact, so the next file can omit its header and serialize against stale columns. Reopening a JSONSink leaves the prior data buffer intact, so the next file includes rows from the previous output session.

Each successful open() should begin an independent output session, matching the fact that the destination file is opened in write mode.

Changes Made

  • Reset CSV header and field-schema state after opening a new file.
  • Reset the JSON row buffer after opening a new file.
  • Add lifecycle regressions that reuse each sink instance across two output paths.
  • Document the fix in the unreleased changelog.

Testing

  • python -m pytest -q tests/detection/test_csv.py tests/detection/test_json.py — 36 passed
  • python -m pytest -q tests/detection — 1,591 passed, 1 skipped
  • All pre-commit hooks passed on the five changed files, including Ruff, mypy, mdformat, and codespell

Additional Notes

No public API changes.

@codecov

codecov Bot commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87%. Comparing base (5f0654e) to head (a3ebbb2).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2459   +/-   ##
=======================================
  Coverage       87%     87%           
=======================================
  Files           85      85           
  Lines        11930   11933    +3     
=======================================
+ Hits         10339   10345    +6     
+ Misses        1591    1588    -3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Bortlesboat
Bortlesboat marked this pull request as ready for review July 27, 2026 19:40
@Bortlesboat
Bortlesboat requested a review from SkalskiP as a code owner July 27, 2026 19:40
@Borda
Borda requested a review from Copilot July 28, 2026 07:28
Borda
Borda previously approved these changes Jul 28, 2026

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.

Pull request overview

This PR fixes lifecycle bugs in Supervision’s detection “sink” utilities by ensuring that reopening an existing sv.CSVSink or sv.JSONSink instance starts a clean output session (matching the fact that files are opened in write mode). This prevents stale per-instance state from leaking into subsequent outputs when a sink object is reused across multiple file paths.

Changes:

  • Reset JSONSink’s in-memory row buffer (self.data) on each successful open().
  • Reset CSVSink’s per-file header/schema state (header_written, field_names) on each successful open().
  • Add regression tests that reuse a single sink instance across two output files, and document the fix in the unreleased changelog.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/supervision/detection/tools/json_sink.py Clears buffered JSON rows on open() so each output file contains only the rows appended in that session.
src/supervision/detection/tools/csv_sink.py Clears CSV header/schema state on open() so each output file gets an appropriate header and column ordering.
tests/detection/test_json.py Adds a lifecycle regression test validating that reopening a JSONSink does not carry over rows.
tests/detection/test_csv.py Adds a lifecycle regression test validating that reopening a CSVSink rewrites header/schema for the new file.
docs/changelog.md Documents the bug fix in the Unreleased “Fixed” section and updates date_modified.

@Borda
Borda merged commit e138f6c into roboflow:develop Jul 28, 2026
23 checks passed
@Borda Borda mentioned this pull request Jul 29, 2026
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.

3 participants