Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialport): data race on the isClosing bool status of the serial port #1009

Merged
merged 3 commits into from
Jan 21, 2025

Conversation

dido18
Copy link
Contributor

@dido18 dido18 commented Jan 16, 2025

Please check if the PR fulfills these requirements

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • Tests for the changes have been added (for bug fixes / features)
  • What kind of change does this PR introduce?
    Fix a DATA RACE on the isClosing variable of the seiral port.
  • What is the current behavior?
    There are two routines that read and write the isClosing variable concurrently.
    • the routine of the open command reads the isClosing variable to verify if it must stop reading from the serial port
    • the routine of the close command writes the isClosing (setting to true) variable to inform that the serial port must be closed.

Steps to reproduce

  • connect a board to the PC
  • launch the cloud-agent locally with task run
  • visit the debug console http://127.0.0.1:8991/
  • type open /dev/ttyACM0 115200
  • type close /dev/ttyACM0
  • the DATA RACE warning appears in the console where the cloud-agent is running:
2025/01/20 10:12:07 stderr: ==================
2025/01/20 10:12:07 stderr: WARNING: DATA RACE
2025/01/20 10:12:07 stderr: Read at 0x00c0002b8b18 by goroutine 61:
2025/01/20 10:12:07 stderr:   main.(*serport).reader()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/serialport.go:88 +0x167
2025/01/20 10:12:07 stderr:   main.spHandlerOpen()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/serialport.go:345 +0x1467
2025/01/20 10:12:07 stderr:   main.checkCmd.gowrap4()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:157 +0x6b
2025/01/20 10:12:07 stderr: 
2025/01/20 10:12:07 stderr: Previous write at 0x00c0002b8b18 by goroutine 66:
2025/01/20 10:12:07 stderr:   main.(*serport).Close()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/serialport.go:351 +0x37
2025/01/20 10:12:07 stderr:   main.spClose()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/serial.go:267 +0xee
2025/01/20 10:12:07 stderr:   main.checkCmd.gowrap5()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:163 +0x44
2025/01/20 10:12:07 stderr: 
2025/01/20 10:12:07 stderr: Goroutine 61 (running) created at:
2025/01/20 10:12:07 stderr:   main.checkCmd()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:157 +0x366
2025/01/20 10:12:07 stderr:   main.(*hub).run()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:111 +0x244
2025/01/20 10:12:07 stderr:   main.loop.gowrap2()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/main.go:412 +0x33
2025/01/20 10:12:07 stderr: 
2025/01/20 10:12:07 stderr: Goroutine 66 (running) created at:
2025/01/20 10:12:07 stderr:   main.checkCmd()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:163 +0x584
2025/01/20 10:12:07 stderr:   main.(*hub).run()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/hub.go:111 +0x244
2025/01/20 10:12:07 stderr:   main.loop.gowrap2()
2025/01/20 10:12:07 stderr:       /home/dido/code/arduino/arduino-create-agent/main.go:412 +0x33
2025/01/20 10:12:07 stderr: ==================
  • What is the new behavior?
    Add an atomic.Bool to control the access of the isClosing status of the serial port.
  • Does this PR introduce a breaking change?
  • Other information:

@dido18 dido18 linked an issue Jan 16, 2025 that may be closed by this pull request
3 tasks
@codecov-commenter
Copy link

codecov-commenter commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 20.14%. Comparing base (67db428) to head (567c865).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
serialport.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1009      +/-   ##
==========================================
- Coverage   20.14%   20.14%   -0.01%     
==========================================
  Files          42       42              
  Lines        3221     3222       +1     
==========================================
  Hits          649      649              
- Misses       2487     2488       +1     
  Partials       85       85              
Flag Coverage Δ
unit 20.14% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dido18 dido18 requested a review from a team January 17, 2025 09:17
@dido18 dido18 changed the title fix(serialport): add mutex for thread-safe access in serialport isClosing fix(serialport): add mutex for thread-safe access of isClosing bool status Jan 17, 2025
@dido18 dido18 self-assigned this Jan 17, 2025
Copy link
Member

@cmaglie cmaglie left a comment

Choose a reason for hiding this comment

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

If we only need to synchronize access to a single variable maybe an atomic.Bool could be better?

@dido18 dido18 requested a review from cmaglie January 20, 2025 09:16
@dido18 dido18 changed the title fix(serialport): add mutex for thread-safe access of isClosing bool status fix(serialport): data race on the isClosing bool status of the serial port Jan 20, 2025
Copy link
Member

@cmaglie cmaglie left a comment

Choose a reason for hiding this comment

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

Retested on Linux, and now seems working OK! ✅

@dido18 dido18 merged commit 1b94ccc into main Jan 21, 2025
42 checks passed
@dido18 dido18 deleted the dara-race-is-closing branch January 21, 2025 08:28
@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data Race on Close command
5 participants