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

proposal: remove shared connections #179

Open
amenzhinsky opened this issue Sep 9, 2019 · 4 comments
Open

proposal: remove shared connections #179

amenzhinsky opened this issue Sep 9, 2019 · 4 comments

Comments

@amenzhinsky
Copy link

I'd propose remove shared connections to make the library less confusing choosing between shared and private connection and generally simplify it.

I've found myself misusing shared connections multiple times, such as closing a connection when it's shared with another part of the code base, subscribing to signals makes receive them other subscribers that don't expect to receive them at all, etc.
It's not possible to pass ConnOption to SystemBus and SessionBus because if it's called later by another piece of code with different options it'll return an inconsistent result.

As for me it's user's responsibility to implement connections sharing and in most situations connection is established once in main or a close to main function that passes it down to other clients.

conn, _ := dbus.SystemBus()
bluez, _ := bluez.Dial(bluez.WithConn(conn))
nm, _ := networkmanager.Dial(networkmanager.WithConn(conn))
...

For now we can add DialSystemBus(opts...) and DialSessionBus(opts...) and mark current functions as deprecated.

Also I'd make calling Auth and Hello implicit in Dial function as well, because they are implementation details, and explicit calling them should produce a warning, for low-level we have NewConn.

@jsouthworth
Copy link
Member

Adding the additional convenience functions is a good idea. I'm not sure I'd support deprecating the older ones, I'd just say they are just special case and document the differences. Deprecation implies that they will be removed in the future, but I wouldn't want to see that as it would cause considerable updates when switching major versions, which we'd like to avoid. In general I'd like to see us avoiding any breaking changes in the future and make this library's external API additive only, avoiding the need to incrementing the module number again.

@probonopd
Copy link

This whole shared vs. private thing also cost me a lot of time, I was never able to use more than one connection (e.g., listen to UDisks2 and send desktop notifications at the same time).

The following works for me, I wonder whether the library could provide a convenience function for it:

	conn, err := dbus.SessionBusPrivate() // When using SessionBusPrivate(), need to follow with Auth(nil) and Hello()
	defer conn.Close()
	if err != nil {
		helpers.PrintError("SessionBusPrivate", err)
		return
	}
	if conn == nil {
		helpers.PrintError("No conn", err)
		return
	}

	if err = conn.Auth(nil); err != nil {
		helpers.PrintError("Auth", err)
		return
	}

	if err = conn.Hello(); err != nil {
		conn.Close()
		helpers.PrintError("Hello", err)
		return
	}

At least all examples could be updated with this.

@jsouthworth
Copy link
Member

Feel free to open pull requests if you would like to contribute these enhancements.

@guelfey
Copy link
Member

guelfey commented Jan 6, 2022

I guess this is mostly closed now with #185. FYI: the shared connections mirror the C API, where they address the case that there are multiple shared libraries that use the bus independently, but should still use the same connection (since multiple connections to the same bus from the same process are possible, but rather wasteful). This also applies to Go bindings in general, but maybe just isn't that relevant unless there actually are multiple libraries in the same process trying to use the same bus. Having easier access to a private connection makes sense, but I'd still keep the shared connections as a concept - maybe we should just make clearer what the implications of using one are.

rkaippully added a commit to rkaippully/spire that referenced this issue Jul 26, 2023
Fixes spiffe#4315

The connection to system bus is shared and should not be closed after
use in order to avoid errors on concurrent usage.

It is typical to share the connection in the same process as per this
explanation:
godbus/dbus#179 (comment)

In case of errors, this shared connection will detect that and attempt
to reconnect: https://github.com/godbus/dbus/blob/v5.1.0/conn.go#L124.
rkaippully added a commit to rkaippully/spire that referenced this issue Jul 26, 2023
Fixes spiffe#4315

The connection to system bus is shared and should not be closed after
use in order to avoid errors on concurrent usage.

It is typical to share the connection in the same process as per this
explanation:
godbus/dbus#179 (comment)

In case of errors, this shared connection will detect that and attempt
to reconnect: https://github.com/godbus/dbus/blob/v5.1.0/conn.go#L124.

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>
amartinezfayo added a commit to spiffe/spire that referenced this issue Aug 23, 2023
* workloadattestor systemd: dbus use of closed network connection

Fixes #4315

The connection to system bus is shared and should not be closed after
use in order to avoid errors on concurrent usage.

It is typical to share the connection in the same process as per this
explanation:
godbus/dbus#179 (comment)

In case of errors, this shared connection will detect that and attempt
to reconnect: https://github.com/godbus/dbus/blob/v5.1.0/conn.go#L124.

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>

* Use a plugin level shared connection

...instead of a global shared one. This ensures that the connection is
never closed by any other library while this plugin is using it.

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>

* Fix lint error - context must be the first parameter

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>

* incorporate review comments

- Implement `Close()` on the plugin to close the DBus connection
- Do not export `DBusConn` method
- Fix a comment

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>

---------

Signed-off-by: Raghu Kaippully <rkaippully@gmail.com>
Co-authored-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
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

4 participants