Closed Veanir closed 2 months ago
@Veanir I'm not sure we can qualify this behavior as a bug.
The thing is, once you call subscribe_async
, then you MUST make sure that you poll the resulting future, or else the system event loop will get stuck, as we put a callback into it that needs to send its data to somewhere, and if you don't poll, it will wait forever.
The easiest way to fix this in your program is to call subscription.next().await
from within your block_on
executor, together with whatever else you do there (I.e. waiting on wifi start).
You are also using block_on
inside block_on
. In 99.99% of the use cases there is no good reason to do that.
Thank you very much for fast reply!
I was trying to do something like:
let mut subscription = self.system_event_loop.subscribe_async::<EspWifiEvent>()?;
loop {
futures::select! {
event = self.channels.wifi_credentials_rx.recv().fuse() => {
let credentials = match event {
Some(event) => event,
None => continue,
};
log::info!("Received WiFi credentials: {:?}", credentials);
self.connect_wifi(credentials).await?;
}
event = subscription.recv().fuse() => {
let event = match event {
Ok(event) => event,
Err(_) => continue,
};
match event {
EspWifiEvent::StaConnected => {
self.state = WifiState::Connected;
self.connect_websocket()?;
}
EspWifiEvent::StaDisconnected => {
self.state = WifiState::Disconnected;
self.ws_client = None;
}
_ => {}
}
}
}
}
But I guess that I would need to keep polling subscription in different thread in order to make it work.
You saved me a lot of time, as I'm still not familiar with underlying mechanisms.
connect_websocket
, as that call might block. Btw why do you mix blocking calls (connect_websocket
) with async calls?Also, just a suggestion: avoid select!
. Use embassy_futures::select
instead.
Btw why do you mix blocking calls (
connect_websocket
) with async calls?
There is no reason other than that I don't really have much experience with async rust... And to be honest not much expertise in anything asynchronous :D I'm just brute forcing my way through async rust
Thanks for suggestions
Unexpected behaviour:
Program gets stuck on
wifi.start().await?
whenever the EspSystemEventLoop is subscribed to with EspEventLoop::subscribe_async().How to reproduce:
Run this slightly modified example.
notes
when subscription is performed after connecting to wifi everything is okay.
Also when subscription is based on callback (EspEventLoop::subscribe()) it works fine even when it's performed before starting wifi.