Skip to content

feat(HikVision): support IDispose interface#783

Merged
ArgoZhang merged 2 commits intomasterfrom
feat-hik
Dec 4, 2025
Merged

feat(HikVision): support IDispose interface#783
ArgoZhang merged 2 commits intomasterfrom
feat-hik

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Dec 4, 2025

Link issues

fixes #782

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Bug Fixes:

  • Fix resource leakage by stopping real-time play, logging out, and destroying the WebVideoCtrl plugin on disposal.

Copilot AI review requested due to automatic review settings December 4, 2025 10:13
@bb-auto bb-auto Bot added the enhancement New feature or request label Dec 4, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Dec 4, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 4, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Implements proper cleanup for HikVision video instances by enhancing the dispose routine to stop playback, log out, and destroy the plugin before removing instance data.

Sequence diagram for updated HikVision dispose cleanup process

sequenceDiagram
    participant Browser
    participant hikvision_js
    participant WebVideoCtrl
    participant Data

    Browser->>hikvision_js: dispose(id)
    activate hikvision_js
    hikvision_js->>hikvision_js: stopRealPlay(id)
    hikvision_js->>hikvision_js: logout(id)
    hikvision_js->>WebVideoCtrl: I_DestroyPlugin()
    hikvision_js->>Data: remove(id)
    deactivate hikvision_js
Loading

File-Level Changes

Change Details Files
Ensure HikVision component disposal performs full cleanup of active sessions and plugin resources.
  • Invoke real-play stop routine during disposal to terminate any active video streams
  • Invoke logout routine during disposal to close active HikVision sessions
  • Call the WebVideoCtrl.I_DestroyPlugin API to destroy the underlying video plugin instance during disposal
  • Retain final cleanup of internal instance data after resource teardown
src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js

Assessment against linked issues

Issue Objective Addressed Explanation
#782 Implement proper disposal logic for the HikVision component so that invoking its dispose/IDispose behavior cleans up active streams, logs out, destroys the plugin, and clears associated data.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit e67fab3 into master Dec 4, 2025
1 of 2 checks passed
@ArgoZhang ArgoZhang deleted the feat-hik branch December 4, 2025 10:13
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Consider guarding the WebVideoCtrl.I_DestroyPlugin() call (and possibly stopRealPlay/logout) with checks to ensure the plugin and session are initialized to avoid errors if dispose is called multiple times or before setup completes.
  • If dispose(id) can be invoked while streaming or login operations are in progress, think about making these teardown calls idempotent or sequencing them with any async operations to prevent race conditions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding the `WebVideoCtrl.I_DestroyPlugin()` call (and possibly `stopRealPlay`/`logout`) with checks to ensure the plugin and session are initialized to avoid errors if `dispose` is called multiple times or before setup completes.
- If `dispose(id)` can be invoked while streaming or login operations are in progress, think about making these teardown calls idempotent or sequencing them with any async operations to prevent race conditions.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.HikVision/wwwroot/hikvision.js:242` </location>
<code_context>
 export function dispose(id) {
+    stopRealPlay(id);
+    logout(id);
+    WebVideoCtrl.I_DestroyPlugin();
+
     Data.remove(id);
</code_context>

<issue_to_address>
**issue (bug_risk):** Destroying the global plugin in a per-id dispose may affect other active sessions.

If `WebVideoCtrl` is a singleton shared across multiple `id`s, calling `I_DestroyPlugin()` here will shut it down for all sessions whenever any one `id` is disposed. That can break other active or subsequent sessions. You may need to only destroy the plugin when the last `id` is disposed, or move plugin lifecycle management outside this per-id `dispose` path.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

export function dispose(id) {
stopRealPlay(id);
logout(id);
WebVideoCtrl.I_DestroyPlugin();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Destroying the global plugin in a per-id dispose may affect other active sessions.

If WebVideoCtrl is a singleton shared across multiple ids, calling I_DestroyPlugin() here will shut it down for all sessions whenever any one id is disposed. That can break other active or subsequent sessions. You may need to only destroy the plugin when the last id is disposed, or move plugin lifecycle management outside this per-id dispose path.

Copy link
Copy Markdown

Copilot AI left a comment

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 implements the IDisposable pattern for the HikVision component by adding cleanup logic to the dispose function. Previously, the dispose function only removed data from the Data store. The changes add proper resource cleanup by stopping real-time playback, logging out from the device, and destroying the plugin instance.

Key Changes:

  • Added calls to stopRealPlay(id) and logout(id) before data removal
  • Added WebVideoCtrl.I_DestroyPlugin() call to destroy the plugin instance
  • Ensures proper cleanup of HikVision resources when components are disposed

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +240 to +242
stopRealPlay(id);
logout(id);
WebVideoCtrl.I_DestroyPlugin();
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

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

The dispose function doesn't wait for asynchronous operations to complete. Both stopRealPlay(id) and logout(id) perform asynchronous operations via WebVideoCtrl.I_Stop and WebVideoCtrl.I_Logout, but dispose doesn't await their completion before calling WebVideoCtrl.I_DestroyPlugin() and Data.remove(id). This can lead to race conditions where the plugin is destroyed or data is removed while cleanup operations are still in progress.

Consider making dispose async and awaiting the operations:

export async function dispose(id) {
    await stopRealPlay(id);
    await logout(id);
    WebVideoCtrl.I_DestroyPlugin();
    
    Data.remove(id);
}

Note: You'll also need to update stopRealPlay to return a Promise for proper awaiting.

Copilot uses AI. Check for mistakes.
Comment on lines +242 to 244
WebVideoCtrl.I_DestroyPlugin();

Data.remove(id);
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

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

WebVideoCtrl.I_DestroyPlugin() appears to destroy the entire plugin instance globally, not just for a specific id. If multiple HikVision components can exist simultaneously (which is suggested by the id parameter pattern throughout the codebase), calling this in dispose(id) will destroy the plugin for all instances, not just the one being disposed.

Consider checking if there are any remaining instances before destroying the plugin:

Data.remove(id);
if (Data.size === 0) {
    WebVideoCtrl.I_DestroyPlugin();
}

Or, if the plugin is designed to be per-instance, ensure proper instance management in the WebVideoCtrl API.

Suggested change
WebVideoCtrl.I_DestroyPlugin();
Data.remove(id);
Data.remove(id);
if (Data.size === 0) {
WebVideoCtrl.I_DestroyPlugin();
}

Copilot uses AI. Check for mistakes.
Comment on lines +240 to +243
stopRealPlay(id);
logout(id);
WebVideoCtrl.I_DestroyPlugin();

Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

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

The dispose function lacks error handling. If Data.get(id) fails (e.g., if the id doesn't exist), both stopRealPlay(id) and logout(id) will throw errors, preventing the cleanup from completing. This could leave resources in an inconsistent state.

Consider adding try-catch blocks or null checks:

export function dispose(id) {
    try {
        stopRealPlay(id);
    } catch (e) {
        console.error('Error stopping real play:', e);
    }
    
    try {
        logout(id);
    } catch (e) {
        console.error('Error logging out:', e);
    }
    
    WebVideoCtrl.I_DestroyPlugin();
    Data.remove(id);
}
Suggested change
stopRealPlay(id);
logout(id);
WebVideoCtrl.I_DestroyPlugin();
try {
stopRealPlay(id);
} catch (e) {
console.error('Error stopping real play:', e);
}
try {
logout(id);
} catch (e) {
console.error('Error logging out:', e);
}
WebVideoCtrl.I_DestroyPlugin();

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(HikVision): support IDispose interface

2 participants