From 1d89392ceffaa58a7ff9de2ce89343efad29a2b4 Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Mon, 30 Nov 2026 16:24:35 +0800 Subject: [PATCH] Reject frames read across a stop and restart of a GVFG channel - Record the channel's run generation when a read begins and refuse delivery if stop and start ran during the read. The frame belonged to the stopped stream and its backend hold had been reset, so the caller's release failed with GVFG_ESTATE and every later read stayed rejected until the next stop. - Return GVFG_ESTATE with a specific last-error message in that case, and document it in the customer API reference. Validation: git diff --cached --check passed. The MSVC Release SDK build passed. On a GIGABYTE SDI VFG100 with a 1080p60 SDI signal, a hardware reproduction that parks a read between capture and delivery left the channel stuck with the previous 3.2.0 code and with 3.1.0, and passed with this change. The Viewer capture journey (start, snapshot, stop, restart) passed on this SDK. A TLA+ model of the frame-hold protocol, kept outside this commit, passes with the reader running across stop and start. SDK version not bumped. Co-Authored-By: Claude Opus 5.5 --- GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp | 18 +++++++++++++++--- .../GVFG_CUSTOMER_API_REFERENCE.md | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp index 8bf977e..d068515 100644 --- a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp +++ b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp @@ -218,7 +218,11 @@ struct gvfg_channel_session_t if (st != GVFG_OK) return st; - running = true; + { + std::lock_guard lock(frameMutex); + ++runGeneration; + running = true; + } return GVFG_OK; } @@ -383,6 +387,7 @@ struct gvfg_channel_session_t return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not running"); const char *stateError = nullptr; + uint64_t readRunGeneration = 0; { std::lock_guard lock(frameMutex); if (frameHeld) @@ -390,7 +395,10 @@ struct gvfg_channel_session_t else if (readInProgress) stateError = "gvfg_read_channel_frame rejected: another read is already in progress"; else + { readInProgress = true; + readRunGeneration = runGeneration; + } } if (stateError) return reject(GVFG_ESTATE, stateError); @@ -478,7 +486,9 @@ struct gvfg_channel_session_t bool stoppedWhileWaiting = false; { std::lock_guard lock(frameMutex); - if (!running.load(std::memory_order_acquire)) + // A stop and start during this read set running back to true, but the + // frame belongs to the stopped stream and its backend hold is gone. + if (!running.load(std::memory_order_acquire) || runGeneration != readRunGeneration) { readInProgress = false; stoppedWhileWaiting = true; @@ -496,7 +506,8 @@ struct gvfg_channel_session_t { if (backendFrameHeld) session->release_frame(); - return GVFG_ESTATE; + return reject(GVFG_ESTATE, + "gvfg_read_channel_frame rejected: capture stopped or restarted during the read"); } updateRuntimeFps(now_ns()); @@ -839,6 +850,7 @@ struct gvfg_channel_session_t bool readInProgress = false; bool frameHeld = false; bool heldFrameNeedsBackendRelease = false; + uint64_t runGeneration = 0; // Protected by frameMutex; bumped by every successful start. uint64_t videoFrameId = 0; gvfg_frame_t heldSessionFrame{}; std::mutex processorMutex; diff --git a/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md b/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md index 141b539..e70678e 100644 --- a/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md +++ b/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md @@ -490,7 +490,7 @@ gvfg_status_t gvfg_read_channel_frame(/* [in] */ gvfg_handle handle, - `timeout_ms`:0 為 non-blocking;`GVFG_TIMEOUT_INFINITE` 為無限等待;其他值為毫秒。 - `GVFG_OK`:成功取得 frame,caller 現在持有一個必須 release 的 token。 - `GVFG_EINVAL`:handle 或 output pointer 為 NULL。 -- `GVFG_ESTATE`:未 running、已有 held frame、已有另一個 read,或等待時被 stop。 +- `GVFG_ESTATE`:未 running、已有 held frame、已有另一個 read,或 read 期間被 stop(包含 stop 後又重新 start;此時不會交出 stop 前的 frame)。 - `GVFG_ETIMEOUT`:期限內沒有 frame。 - `GVFG_EBUSY` 表示 capture resource 正在使用中;`GVFG_EIO` 表示 capture 或 I/O 失敗; `GVFG_ENOTSUP` 表示 SDK 不支援該格式。