Skip to content

Conversation

@AsCress
Copy link
Collaborator

@AsCress AsCress commented Aug 16, 2025

Fixes #2849.

Screenshots / Recordings

Screen_recording_20250816_094216.mp4

Summary by Sourcery

Add processing guard to BoardStateProvider to prevent overlapping initialization and USB connection attempts

Bug Fixes:

  • Introduce private _isProcessing flag to prevent concurrent calls to initialize()
  • Apply the same guard to the USB attached event handler to avoid overlapping connection attempts

@AsCress AsCress self-assigned this Aug 16, 2025
@AsCress AsCress added Fix Solution to an existing issue in app flutter labels Aug 16, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 16, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduce a private _isProcessing flag in BoardStateProvider to guard against concurrent initialization and USB connection attempts, ensuring only one processing sequence runs at a time.

Sequence diagram for guarded BoardStateProvider initialization and USB event handling

sequenceDiagram
participant BSP as BoardStateProvider
participant SLC as ScienceLabCommon
participant USB as UsbSerial
participant Event as UsbEvent

BSP->>BSP: initialize()
alt _isProcessing is false
    BSP->>BSP: set _isProcessing = true
    BSP->>SLC: initialize()
    BSP->>SLC: openDevice()
    BSP->>BSP: setPSLabVersionIDs()
    BSP->>BSP: set _isProcessing = false
    alt autoStart is true
        USB->>BSP: usbEventStream.listen()
        Event->>BSP: ACTION_USB_ATTACHED
        alt _isProcessing is false
            BSP->>BSP: set _isProcessing = true
            BSP->>BSP: attemptToConnectPSLab()
            BSP->>SLC: openDevice()
            BSP->>BSP: setPSLabVersionIDs()
            BSP->>BSP: set _isProcessing = false
        end
        Event->>BSP: ACTION_USB_DETACHED
        BSP->>SLC: isWiFiConnected()
        alt not WiFi connected
            BSP->>BSP: handle detachment
        end
    end
end
Loading

Class diagram for updated BoardStateProvider initialization logic

classDiagram
class BoardStateProvider {
  int pslabVersion
  String exportFormat
  bool autoStart
  bool _isProcessing
  ScienceLabCommon scienceLabCommon
  Future<void> initialize()
  void setPSLabVersionIDs()
}
BoardStateProvider --> ScienceLabCommon
Loading

File-Level Changes

Change Details Files
Prevent overlapping initialization and USB connection flows by adding a processing guard
  • Add private boolean _isProcessing defaulting to false
  • Early return in initialize() if already processing, and set/reset the flag around async setup
  • Apply same guard and flag management in USB attached event listener before and after connection logic
lib/providers/board_state_provider.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#2849 Fix the bug where the PSLab version string is not read properly when opening the app and connecting the board.

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

Copy link
Contributor

@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:

  • Make sure to reset _isProcessing in all code paths (including failed attempts and exceptions) or wrap it in a try/finally to avoid locking further initialization calls.
  • Store and cancel the UsbSerial.usbEventStream subscription in dispose() to prevent potential memory leaks.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Make sure to reset `_isProcessing` in all code paths (including failed attempts and exceptions) or wrap it in a try/finally to avoid locking further initialization calls.
- Store and cancel the `UsbSerial.usbEventStream` subscription in dispose() to prevent potential memory leaks.

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.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 16, 2025

@marcnause marcnause enabled auto-merge (rebase) August 16, 2025 21:53
@marcnause marcnause merged commit 3465147 into fossasia:flutter Aug 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Fix Solution to an existing issue in app flutter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PSLab Version String isn't read properly

2 participants