-
Notifications
You must be signed in to change notification settings - Fork 389
Avoid crashing after shutting down WiFi #4761
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
Open
bjoernQ
wants to merge
3
commits into
esp-rs:main
Choose a base branch
from
bjoernQ:fix-crash-shutting-down-wifi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−67
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| //! Check that trying to send after drop doesn't crash | ||
|
|
||
| //% FEATURES: esp-radio esp-radio/wifi esp-radio/unstable esp-hal/unstable | ||
| //% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32s2 esp32s3 | ||
|
|
||
| #![no_std] | ||
| #![no_main] | ||
|
|
||
| use embassy_executor::Spawner; | ||
| use esp_alloc as _; | ||
| use esp_backtrace as _; | ||
| use esp_hal::{ | ||
| clock::CpuClock, | ||
| interrupt::software::SoftwareInterruptControl, | ||
| ram, | ||
| timer::timg::TimerGroup, | ||
| }; | ||
| use esp_println::println; | ||
| use esp_radio::wifi::{ModeConfig, sta::StationConfig}; | ||
| use smoltcp::phy::Device; | ||
|
|
||
| esp_bootloader_esp_idf::esp_app_desc!(); | ||
|
|
||
| const SSID: &str = env!("SSID"); | ||
| const PASSWORD: &str = env!("PASSWORD"); | ||
|
|
||
| #[esp_rtos::main] | ||
| async fn main(_spawner: Spawner) { | ||
| esp_println::logger::init_logger_from_env(); | ||
| let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); | ||
| let peripherals = esp_hal::init(config); | ||
|
|
||
| esp_alloc::heap_allocator!(size: 32 * 1024); | ||
| // add some more RAM | ||
| esp_alloc::heap_allocator!(#[ram(reclaimed)] size: 64 * 1024); | ||
|
|
||
| let timg0 = TimerGroup::new(peripherals.TIMG0); | ||
| let sw_int = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); | ||
| esp_rtos::start(timg0.timer0, sw_int.software_interrupt0); | ||
|
|
||
| let mut wifi = peripherals.WIFI; | ||
| { | ||
| let (mut controller, interfaces) = | ||
| esp_radio::wifi::new(wifi.reborrow(), Default::default()).unwrap(); | ||
|
|
||
| let mut wifi_interface = interfaces.station; | ||
|
|
||
| let token = wifi_interface.transmit(smoltcp::time::Instant::from_millis( | ||
| esp_hal::time::Instant::now() | ||
| .duration_since_epoch() | ||
| .as_millis() as i64, | ||
| )); | ||
| assert!(matches!(token, None)); | ||
|
|
||
| let station_config = ModeConfig::Station( | ||
| StationConfig::default() | ||
| .with_ssid(SSID.into()) | ||
| .with_password(PASSWORD.into()), | ||
| ); | ||
| controller.set_config(&station_config).unwrap(); | ||
| controller.start_async().await.unwrap(); | ||
| controller.connect_async().await.unwrap(); | ||
|
|
||
| let token = wifi_interface.transmit(smoltcp::time::Instant::from_millis( | ||
| esp_hal::time::Instant::now() | ||
| .duration_since_epoch() | ||
| .as_millis() as i64, | ||
| )); | ||
|
|
||
| println!("got token {}", token.is_some()); | ||
|
|
||
| core::mem::drop(controller); | ||
|
|
||
| if let Some(token) = token { | ||
| token.consume_token(10, |tx| tx.fill(0)); | ||
| println!("survived consumer_token"); | ||
| } else { | ||
| println!("no token"); | ||
| } | ||
|
|
||
| let token = wifi_interface.transmit(smoltcp::time::Instant::from_millis( | ||
| esp_hal::time::Instant::now() | ||
| .duration_since_epoch() | ||
| .as_millis() as i64, | ||
| )); | ||
| assert!(matches!(token, None)); | ||
| } | ||
|
|
||
| { | ||
| let (mut controller, interfaces) = | ||
| esp_radio::wifi::new(wifi.reborrow(), Default::default()).unwrap(); | ||
|
|
||
| let station_config = ModeConfig::Station( | ||
| StationConfig::default() | ||
| .with_ssid(SSID.into()) | ||
| .with_password(PASSWORD.into()), | ||
| ); | ||
| controller.set_config(&station_config).unwrap(); | ||
| controller.start_async().await.unwrap(); | ||
| controller.connect_async().await.unwrap(); | ||
|
|
||
| let mut wifi_interface = interfaces.station; | ||
|
|
||
| let token = wifi_interface.transmit(smoltcp::time::Instant::from_millis( | ||
| esp_hal::time::Instant::now() | ||
| .duration_since_epoch() | ||
| .as_millis() as i64, | ||
| )); | ||
|
|
||
| println!("got token {}", token.is_some()); | ||
|
|
||
| if let Some(token) = token { | ||
| token.consume_token(10, |tx| tx.fill(0)); | ||
| println!("tx done"); | ||
| } else { | ||
| println!("no token"); | ||
| } | ||
| } | ||
|
|
||
| println!("Done"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this limit us to one in-flight packet at a time? I think we should only give back the semaphore in
esp_wifi_tx_done_cb, or on an error, basically insidedecrement_inflight_counteror when we would otherwise call it.It's also a bit too late to lock here, we've already given the token, we should be able to send the packet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't limit us to one? - the assumption (which is hard to prove) is that the blobs handle the inflight tx gracefully during tear-down - we just can't schedule another transmission once the buffers etc. are gone.
If that assumption is true we might not even end up in
esp_wifi_tx_done_cb- oh wait - that would mean we should reset the inflight counter before creating a (new) controller 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it sounds much easier and robust to prevent shutdown if there are any tranmissions running 😅 With the added caveat that attempting to tear the whole system down should block any new packets from being sent, or network traffic can prevent the driver from being shut down.