Skip to content

The Characteristic::write() function can hang #182

@alistair23

Description

@alistair23

Sometimes the Characteristic::write() just never returns and hangs the entire Tokio runtime.

For example I call the function from a non async context like this

tokio::task::block_in_place(|| {
	let rt = tokio::runtime::Handle::current();
	rt.block_on(characteristic.write(data))
})

That generally works, but ever 50 or so calls the function just hangs the entire process.

As block_in_place() can have some issues I've also tried spawning a Tokio task to handle the write

        // Create write queue channel
        let (write_tx, mut write_rx) = mpsc::unbounded_channel::<WriteRequest>();

        // Spawn write worker thread
        tokio::spawn(async move {
            println!("📝 BLE Write worker thread started");
            while let Some(req) = write_rx.recv().await {
                let result = req.characteristic.write(&req.data).await;
                let _ = req.response_tx.send(result);
            }
            panic!("📝 BLE Write worker thread stopped");
        }); 

and then pass the write to the task like this

            self.write_tx.send(write_req)?;

            // Block waiting for the worker thread to complete the write
            let result = tokio::task::block_in_place(|| {
                tokio::runtime::Handle::current().block_on(response_rx)
            })

which is more robust, but eventuall also hangs.

tokio-console doesn't offer much as I can't see where in Characteristic::write() the hang occurs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions