purejava/keepassxc-proxy-access

Cannot close connection to KeePassXC

Closed this issue · 5 comments

Please agree to the following

Summary

After connecting the connection cannot be closed.

What software is involved?

  • Operating System: Linux Mint 20.3 Cinnamon
  • keepassxc-proxy-access: 1.1.1
  • KeePassXC: 2.7.1
  • Eclipse: 2021-12
  • Java: 17

Steps to Reproduce

  1. Create a new KeepassProxyAccess
  2. Connect
KeepassProxyAccess kpa = new KeepassProxyAccess();
kpa.connect();

Expected Behavior

Option to close the connection or as described in the code

The closing of the connection is handled automatically.

Actual Behavior

Program runs and the connection is never closed. I don't know when this connection should be closed, had a program running for ten minutes and nothing happened.

Reproducibility

Always

Relevant Log Output

No response

Anything else?

Please let me know if you need more information. Great project and thanks a lot for your effort.

The connection is closed when you end your program as Connection imlements AutoClosable:

public abstract class Connection implements AutoCloseable {

The underlying socket (on Linux and Mac) or named pipe (on Windows) to KeePassXC is closed then:


readPipe.close();
writePipe.close();

IMHO there is no need to close a connection manually.

@purejava Thank you for your quick reply and your information, will try again on Monday and let you know if now works. ;)

@purejava it's not true that a class implementing AutoCloseable does close automatically when a program ends. Therefore it needs to be declared in a try-with-resources block.

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html

public interface AutoCloseable
An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.

Furthermore, this library maybe is used in some long-running applications where it's not required for the connection to stay open all the time. So there should be a way to close the connection manually.

Thanks @Flofler for that hint and the link to the docs.

I'll release a new version of the library soon and add an option to manually close the connection.

Fixed for the upcoming release that will provide:

/**
* Close the connection to the socket (for Linux and Mac) or the named pipe (for Windows) respectively.
*
* @return True, in case the connection was closed without an error, false otherwise.
*/
public boolean closeConnection() {
try {
connection.terminateConnection();
return true;
} catch (IOException e) {
log.error(e.toString(), e.getCause());
return false;
}
}