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

SSL connection problem #168

Closed
mgaido91 opened this issue May 30, 2015 · 10 comments
Closed

SSL connection problem #168

mgaido91 opened this issue May 30, 2015 · 10 comments

Comments

@mgaido91
Copy link

Hello,

I'm facing big problem while trying to connect to a DB using SSL and your library...
I've tried many connection strings, but none works.
Do you know what can I do? In fact, I'm getting this exception:

java.sql.SQLException: Connection Error: java.lang.IllegalStateException at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:189) at com.impossibl.postgres.jdbc.PGDriver.connect(PGDriver.java:77) at com.impossibl.postgres.jdbc.PGDriver.connect(PGDriver.java:52) at java.sql.DriverManager.getConnection(DriverManager.java:179) at it.polito.mobile.testpostgres.MainActivity$1.doInBackground(MainActivity.java:60) at it.polito.mobile.testpostgres.MainActivity$1.doInBackground(MainActivity.java:35) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.io.IOException: java.lang.IllegalStateException at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.translateConnectionException(ProtocolFactoryImpl.java:285) at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:199) at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:90) at com.impossibl.postgres.system.BasicContext.<init>(BasicContext.java:130) at com.impossibl.postgres.jdbc.PGConnectionImpl.<init>(PGConnectionImpl.java:185) at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:180) ... 11 more Caused by: java.lang.IllegalStateException at io.netty.handler.ssl.SslHandler$LazyChannelPromise.executor(SslHandler.java:1491) at io.netty.util.concurrent.DefaultPromise.checkDeadLock(DefaultPromise.java:388) at io.netty.util.concurrent.DefaultPromise.awaitUninterruptibly(DefaultPromise.java:283) at io.netty.util.concurrent.DefaultPromise.syncUninterruptibly(DefaultPromise.java:225) at io.netty.util.concurrent.DefaultPromise.syncUninterruptibly(DefaultPromise.java:32) at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:132) ... 15 more

@harbulot
Copy link

I can also reproduce the problem when getting a connection with SSL mode Require fails:

    Class.forName("com.impossibl.postgres.jdbc.PGDriver");
    String url = "jdbc:pgsql://host.example/test?ssl.mode=Require";
    Connection conn = DriverManager.getConnection(url, "test", "...");

This produces this exception (most of the time):

java.sql.SQLException: Connection Error: java.lang.IllegalStateException
    at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:189)
    at com.impossibl.postgres.jdbc.PGDriver.connect(PGDriver.java:77)
    at com.impossibl.postgres.jdbc.PGDriver.connect(PGDriver.java:1)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at test.MyTest.main(MyTest.java:58)
Caused by: java.io.IOException: java.lang.IllegalStateException
    at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.translateConnectionException(ProtocolFactoryImpl.java:287)
    at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:201)
    at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:91)
    at com.impossibl.postgres.system.BasicContext.<init>(BasicContext.java:130)
    at com.impossibl.postgres.jdbc.PGConnectionImpl.<init>(PGConnectionImpl.java:185)
    at com.impossibl.postgres.jdbc.ConnectionUtil.createConnection(ConnectionUtil.java:180)
    ... 6 more
Caused by: java.lang.IllegalStateException
    at io.netty.handler.ssl.SslHandler$LazyChannelPromise.executor(SslHandler.java:1527)
    at io.netty.util.concurrent.DefaultPromise.checkDeadLock(DefaultPromise.java:388)
    at io.netty.util.concurrent.DefaultPromise.awaitUninterruptibly(DefaultPromise.java:283)
    at io.netty.util.concurrent.DefaultPromise.syncUninterruptibly(DefaultPromise.java:225)
    at io.netty.util.concurrent.DefaultPromise.syncUninterruptibly(DefaultPromise.java:32)
    at com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl.connect(ProtocolFactoryImpl.java:134)
    ... 10 more

This is caused by the call to syncUninterruptibly:

sslHandler.handshakeFuture().syncUninterruptibly();

This is almost certainly due to a race condition regarding when ctx is set in Netty's SslHandler when it's used in LazyChannelPromise.
In SslHandler, this.ctx is only ever set in handlerAdded, which is called via an EventExcecutor (so not necessarily called before syncUninterruptibly() is called).

A slight delay in com.impossibl.postgres.protocol.v30.ProtocolFactoryImpl seems to prevent this exception:

Thread.sleep(10);
sslHandler.handshakeFuture().syncUninterruptibly();

(This is obviously not the correct way to fix this, but this seems to confirm the cause of the problem.)

@harbulot
Copy link

I should add that I haven't been able to reproduce the problem when the client is running on a Linux machine, but I'm able to reproduce it regularly from a Windows machine. Both were running the latest Oracle Java 8 release.

@mgaido91
Copy link
Author

I'm running the client from an Android emulator, thus it seems that only linux machines are supported...

@kdubb
Copy link
Member

kdubb commented May 31, 2015

@normanmaurer is @harbulot issue/solution a Netty problem or are we initializing it incorrectly?

@kdubb kdubb closed this as completed Jun 2, 2015
@kdubb kdubb reopened this Jun 2, 2015
@jesperpedersen
Copy link
Contributor

@harbulot Did you report this to the Netty project ?

@normanmaurer
Copy link
Contributor

Let me check if there is a bug in netty

@normanmaurer
Copy link
Contributor

@jesperpedersen @harbulot @kdubb it is a netty bug... Let me fix it there

@jesperpedersen
Copy link
Contributor

@normanmaurer Thanks !

normanmaurer added a commit to netty/netty that referenced this issue Jun 6, 2015
Motivation:

If the handlerAdded(...) callback was not called, the checkDeadLock() of the handshakeFuture will produce an IllegalStateException.
This was first reported at impossibl/pgjdbc-ng#168 .

Modifications:

Pass deadlock check if ctx is null

Result:

No more race and so IllegalStateException.
normanmaurer added a commit to netty/netty that referenced this issue Jun 8, 2015
Motivation:

If the handlerAdded(...) callback was not called, the checkDeadLock() of the handshakeFuture will produce an IllegalStateException.
This was first reported at impossibl/pgjdbc-ng#168 .

Modifications:

Pass deadlock check if ctx is null

Result:

No more race and so IllegalStateException.
normanmaurer added a commit to netty/netty that referenced this issue Jun 8, 2015
Motivation:

If the handlerAdded(...) callback was not called, the checkDeadLock() of the handshakeFuture will produce an IllegalStateException.
This was first reported at impossibl/pgjdbc-ng#168 .

Modifications:

Pass deadlock check if ctx is null

Result:

No more race and so IllegalStateException.
normanmaurer added a commit to netty/netty that referenced this issue Jun 8, 2015
Motivation:

If the handlerAdded(...) callback was not called, the checkDeadLock() of the handshakeFuture will produce an IllegalStateException.
This was first reported at impossibl/pgjdbc-ng#168 .

Modifications:

Pass deadlock check if ctx is null

Result:

No more race and so IllegalStateException.
@normanmaurer
Copy link
Contributor

Fixed netty/netty#3868. Will be part of netty 4.0.29.Final

@kdubb
Copy link
Member

kdubb commented Jul 13, 2016

Fixed by upgrade to Netty

@kdubb kdubb closed this as completed Jul 13, 2016
pulllock pushed a commit to pulllock/netty that referenced this issue Oct 19, 2023
Motivation:

If the handlerAdded(...) callback was not called, the checkDeadLock() of the handshakeFuture will produce an IllegalStateException.
This was first reported at impossibl/pgjdbc-ng#168 .

Modifications:

Pass deadlock check if ctx is null

Result:

No more race and so IllegalStateException.
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

5 participants