Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

[Bug] Unsafe threads #136

Closed
hopeyen opened this issue May 11, 2023 · 0 comments
Closed

[Bug] Unsafe threads #136

hopeyen opened this issue May 11, 2023 · 0 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@hopeyen
Copy link
Collaborator

hopeyen commented May 11, 2023

Problem statement

We ran into situations in which the radio gets stuck or unable to respond to query requests. For instance,

We might somehow introduced data races (this should be nearly impossible in Rust, very curious to find out the exact cause). To start with, our current practice of using global variables wrapped in OnceCell and Arc with std::sync::mutex or tokio::sync::mutex might not be effectively preventing deadlock possibilities.

Expectation proposal

In theory, using a Mutex to access a global variable before passing it into async functions can be a valid approach to ensure safe concurrent access.

  • Review the approach to access global variables.
  • Redesign the concurrent aspects; Potentially create a global struct to contain all the variables
  • Make sure that async tasks acquire and release locks asynchronously and do not access global var directly.
  • Add timeouts to async functions to avoid threads sitting around forever

Alternative Considerations

  • Use RwLock: for global variable that is read-heavy and has infrequent writes, as RwLock multiple readers or a single writer.
  • Atomic Types: for simple primitive or atomic type.
  • Message Passing: to communicate and share data between async tasks.(tokio::sync::mpsc) to send data between tasks, as MPSC ensures that only one task has ownership of the data at a time and preventing concurrent access issues.
  • Setup tokio_console
@hopeyen hopeyen self-assigned this May 11, 2023
@hopeyen hopeyen added the bug Something isn't working label May 11, 2023
@hopeyen hopeyen added this to the 0.3.0 milestone May 11, 2023
@hopeyen hopeyen changed the title [Bug] Unsafe concurrent access of global variables in async functions [Bug] Unsafe threads May 11, 2023
@hopeyen hopeyen closed this as completed Jun 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant