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

Closing SimpleCollection does not stop DBusConnection #26

Closed
mhogerheijde opened this issue Apr 11, 2021 · 2 comments
Closed

Closing SimpleCollection does not stop DBusConnection #26

mhogerheijde opened this issue Apr 11, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@mhogerheijde
Copy link

Consider the following application:

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.freedesktop.secret.simple.SimpleCollection;

import static java.util.Optional.*;

public class Application {
  public static void main(String[] args) throws IOException {
    var collection = new SimpleCollection("My Collection", null);

    var attributes = Map.of("example", "secret");

    ofNullable(collection.getItems(attributes))
      .flatMap(matchingPaths -> matchingPaths.stream().findFirst())
      .flatMap(pathToSecret -> ofNullable(collection.getSecret(pathToSecret)))
      .ifPresentOrElse(
        secret -> System.out.println(new String(secret)),
        () -> System.out.println("Not found")
      );

    collection.lock();
    collection.close();
    System.out.println("-- Locked & closed the collection");
  }
}

I expect this application to exit after printing the last line "-- Locked & closed the collection" to the console.

The program doesn't stop, however. The Debugger shows that the DBusConnection thread is still running.

Screenshot from 2021-04-11 21-33-03

I'm writing a CLI program in which I would like to make use of this library to connect to the Gnome Keyring. I'd like to prevent having to explicitly call a System.exit(0) since I use a library to manage the lifecycle of parsing options and calling the appropriate functions after parsing the options.

It does work, however, to explicitly call System.exit(0).

@swiesend swiesend added the bug Something isn't working label Apr 15, 2021
@swiesend swiesend self-assigned this Apr 15, 2021
@swiesend
Copy link
Owner

swiesend commented Apr 16, 2021

Hey @mhogerheijde,

thanks for pointing out this inconsistency and giving a minimal working example.

c386feb will close the dbus connection immediately or at least calls the DBusConnection.close() (dbus-java, 3.3.0) method, which eventually will close all threads. With the fix I could observe that the connection states connected = false, but that the DBusConnection or other DBus {Reader,Sender,Worker} Thread-* threads were sometimes still RUNNING or on WAIT.

Before this fix DBusConnection.close() was only called with a shutdown hook as the DBusConnection is initialized as private static within the SimpleCollection. So the dbus connection were always closed at the end of the lifetime of the SimpleCollection.

I hope this satisfies your expectations, otherwise we will have to track this behavior further down and I guess have to open an issue with dbus-java.

@mhogerheijde
Copy link
Author

Cool, thanks! I'm taking two weeks off, so I will probably not use the fix before the end of my staycation, but I'll try it asap and let you know if this works for us.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants