Different behaviour when chaining vs using intermediate value

Hello, I am going crazy because I don't understand what it happens under the hood.

The following is a method to start a ble scan through the dbus using .
I am storing the dbus_connection in a mutex as a field of my struct.

If I do it that way, the scan starts and it works:

 pub fn start_scan(&self) {
        let connection = self.dbus_connection.lock().unwrap();

            .with_proxy(BLUEZ_DBUS_DESTINATION, "/org/bluez/hci0", STANDARD_TIMEOUT)
            .start_discovery().expect("Error starting discovery");

However, if I remove the intermediate variable, nothing append and the scan never start.

 pub fn start_scan(&self) {
            .with_proxy(BLUEZ_DBUS_DESTINATION, "/org/bluez/hci0", STANDARD_TIMEOUT)
            .start_discovery().expect("Error starting discovery");

I can't understand because, if there were some invalid value inside of the unlocked mutex the program should panic rather than doing nothing at all.

I am using the dbus-rs crate to achieve dbus communication.

What are the difference between the two code blocks ? What is happening ?
Thanks for your help

Could you possibly make a minimal, reproducible code example for this? Or share your original crate, if it's public?

There are differences in your two pieces of code, but if that's all your function is doing, then it shouldn't make any difference in practice. The biggest/only difference I know of is when the lock is unwrapped: if you had any statements after starting the discovering, in your former example, the lock would stay locked during those statements, and in your latter example, it'd be immediately unlocked after calling start_discovery.

The other possibility is that some code is exhibiting Undefined Behavior, either in your crate or in one of your dependencies, and that is causing intermittent miscompilation. Weird, completely unrelated code making things work or not work is a hallmark of unsound code and undefined behavior.

Thanks for your response.
I have created a repo to host a mwe (Maybe not minimal enough):

Something that might be important: I haven't written the start_discovery method. It is automatically generated using a tool provided by the dbus-rs crate author. Also, I have slightly modified it (at Deref target level) to fit my need.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.