Skip to content
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

SystemBusPrivate doesn't return a working conn. #15

Closed
james-lawrence opened this issue Mar 31, 2015 · 20 comments
Closed

SystemBusPrivate doesn't return a working conn. #15

james-lawrence opened this issue Mar 31, 2015 · 20 comments

Comments

@james-lawrence
Copy link

to get a working conn need to invoke:

    conn, err := dbus.SystemBusPrivate()
    if err != nil {
        return err
    }
    if err = conn.Auth(nil); err != nil {
        conn.Close()
        return err
    }
    if err = conn.Hello(); err != nil {
        conn.Close()
        return  err
    }
@philips
Copy link
Contributor

philips commented Mar 31, 2015

The problem is that you don't want to have to do the Auth and Hello?

@james-lawrence
Copy link
Author

The problem is shouldn't have to. expected it to return a working connection. instead I got a broken one without any documentation as to why on the method.

@purpleidea
Copy link

I just figured out there were private/shared buses for both session and system. This explains why different parts of the code are receiving each other's messages! In any case, I couldn't get the PrivateBus to work, until I dug through the code and found this:

func SystemBus() (conn *Conn, err error) {
    systemBusLck.Lock()
    defer systemBusLck.Unlock()
    if systemBus != nil {
        return systemBus, nil
    }
    defer func() {
        if conn != nil {
            systemBus = conn
        }
    }()
    conn, err = SystemBusPrivate()
    if err != nil {
        return
    }
    if err = conn.Auth(nil); err != nil {
        conn.Close()
        conn = nil
        return
    }
    if err = conn.Hello(); err != nil {
        conn.Close()
        conn = nil
    }
    return
}

Which is approximately what @james-lawrence is saying.

So I do agree some doc/api fixes are in order... Will test this all tomorrow!

@purpleidea
Copy link

Incidentally, if you're not using private buses, then if you close the connection, everyone who is on the shared bus will see it closed!

@kyrofa
Copy link
Contributor

kyrofa commented Feb 22, 2016

@purpleidea indeed, the docs for Close() say to not call it on shared connections.

@purpleidea
Copy link

@philips would you accept a patch to either:

  1. Change the API so that SystemBusPrivate returns something usable, and creates a SystemBusBase which does what SystemBusPrivate does?

or
2) Add a SystemBusPrivateReady method that returns the usable thing we expect, similar to SystemBus.

I'd prefer the first choice, because it fixes the API to make it most logical.

LMK

@purpleidea
Copy link

I have this snippet in my code in the meantime now:

func SystemBusPrivateUsable() (conn *dbus.Conn, err error) {
    conn, err = dbus.SystemBusPrivate()
    if err != nil {
        return nil, err
    }
    if err = conn.Auth(nil); err != nil {
        conn.Close()
        conn = nil
        return
    }
    if err = conn.Hello(); err != nil {
        conn.Close()
        conn = nil
    }
    return conn, nil // success
}

Happy to merge it upstream if someone is interested.

@philips
Copy link
Contributor

philips commented Feb 22, 2016

@purpleidea Is there another dbus API that we can compare this Private/Base solution to?

@purpleidea
Copy link

@purpleidea Is there another dbus API that we can compare this Private/Base solution to?

@philips I'm sorry, I'm not sure if I understand what you're asking. The point is to make using the "Private" version of the bus a drop in replacement for the regular dbus.SystemBus() call.

@philips
Copy link
Contributor

philips commented Feb 22, 2016

@purpleidea I am asking how does a C or python or Java library handle this?

@purpleidea
Copy link

On Mon, Feb 22, 2016 at 5:34 PM, Brandon Philips notifications@github.com
wrote:

@purpleidea https://github.com/purpleidea I am asking how does a C or
python or Java library handle this?

Oh, typically AFAIK there is a private (true/false) keyword on the main
initializer... This would also break API. I'm actually file with the two
bus objects, but as I said, the naming is confusing, and we lack the easy
private option. Python:

https://dbus.freedesktop.org/doc/dbus-python/api/dbus._dbus.Bus-class.html#__new__

@tomkcook
Copy link
Contributor

Just to say, I have also been burned by this. At least an update to the API docs to say, "This doesn't return a working connection in the same way SystemBus() does, you need to call Auth() and Hello() on it before you use it" would be nice.

@james-lawrence
Copy link
Author

I agree, that is the minimum that should be done.

@ozmy
Copy link

ozmy commented Feb 21, 2019

I ask if it is possible to put in example a working sample code for dbus.SystemBusPrivate. Thank you.

@jsouthworth
Copy link
Member

Are the changes in #140 sufficient?

@james-lawrence
Copy link
Author

believe so

@ozmy
Copy link

ozmy commented Feb 25, 2019

managed to figure it out
ip address must be passed as a separate argument.
Thank.

@jsouthworth
Copy link
Member

The request was to add documentation on how to use SystemBusPrivate. See the example here: https://godoc.org/github.com/godbus/dbus#SystemBusPrivate

@tomkcook
Copy link
Contributor

tomkcook commented Mar 4, 2019

Stumbled across the changes in the godoc today. Looks good, thank you.

@guelfey
Copy link
Member

guelfey commented Apr 9, 2023

Adressed in #140.

@guelfey guelfey closed this as completed Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants