Skip to content

Commit 5df5cc4

Browse files
author
Artur Bisharyan
committed
Fix one-nio server jvm crash on ssl reconfigure
1 parent 5da84e2 commit 5df5cc4

4 files changed

Lines changed: 402 additions & 5 deletions

File tree

src/main/java/one/nio/net/NativeSslSocket.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package one.nio.net;
1818

1919
import java.io.IOException;
20-
import java.io.RandomAccessFile;
2120

2221
class NativeSslSocket extends NativeSocket {
23-
NativeSslContext context;
22+
volatile NativeSslContext context;
23+
private volatile NativeSslContext previousContext;
2424
long ssl;
2525

2626
private volatile boolean isEarlyDataAccepted = false;
@@ -39,6 +39,11 @@ public synchronized void close() {
3939
sslFree(ssl);
4040
ssl = 0;
4141
}
42+
NativeSslContext prev = previousContext;
43+
if (prev != null) {
44+
prev.close();
45+
previousContext = null;
46+
}
4247
super.close();
4348
}
4449

@@ -64,6 +69,22 @@ public SslContext getSslContext() {
6469
return context;
6570
}
6671

72+
@Override
73+
public void setSslContext(SslContext newContext) throws IOException {
74+
NativeSslContext nativeCtx = (NativeSslContext) newContext;
75+
76+
// Close the context from the PREVIOUS reconfigure.
77+
// By now it is safe: at least one full volatile writeread cycle has passed
78+
NativeSslContext prev = this.previousContext;
79+
if (prev != null) {
80+
prev.close();
81+
}
82+
83+
NativeSslContext old = this.context;
84+
this.context = nativeCtx;
85+
this.previousContext = old; // defer context close to next reconfigure/socket close
86+
}
87+
6788
@Override
6889
@SuppressWarnings("unchecked")
6990
public Object getSslOption(SslOption option) {
@@ -93,6 +114,7 @@ public Object getSslOption(SslOption option) {
93114
}
94115
return null;
95116
}
117+
96118
@Override
97119
public synchronized native void handshake(String sniHostName) throws IOException;
98120

@@ -128,16 +150,22 @@ private boolean sslHandshakeDone() {
128150
}
129151

130152
private synchronized native byte[] sslPeerCertificate();
153+
131154
private synchronized native Object[] sslPeerCertificateChain();
155+
132156
private synchronized native String sslCertName(int which);
157+
133158
private synchronized native String sslVerifyResult();
134159

135160
private synchronized native boolean sslSessionReused();
161+
136162
private synchronized native int sslSessionTicket();
137163

138164
private synchronized native String sslCurrentCipher();
165+
139166
private synchronized native boolean sslCanUseSendfile();
140167

141168
static native long sslNew(int fd, long ctx, boolean serverMode) throws IOException;
169+
142170
static native void sslFree(long ssl);
143171
}

src/main/java/one/nio/net/Socket.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public void setThinLinearTimeouts(boolean thinLto) {}
156156
public abstract Socket sslWrap(SslContext context) throws IOException;
157157
public abstract Socket sslUnwrap();
158158
public abstract SslContext getSslContext();
159+
public void setSslContext(SslContext newContext) throws IOException {}
159160
public abstract <T> T getSslOption(SslOption<T> option);
160161

161162
public Socket acceptNonBlocking() throws IOException {

src/main/java/one/nio/server/acceptor/AcceptorSupport.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ public static void reconfigureSocket(Socket socket, AcceptorConfig config) throw
5858
socket.setReuseAddr(true, config.reusePort);
5959
socket.setThinLinearTimeouts(config.thinLto);
6060

61-
SslContext sslContext = socket.getSslContext();
62-
if (sslContext != null && config.ssl != null) {
63-
sslContext.configure(config.ssl);
61+
SslContext oldContext = socket.getSslContext();
62+
if (oldContext != null && config.ssl != null) {
63+
SslContext newContext = SslContext.create();
64+
newContext.configure(config.ssl);
65+
socket.setSslContext(newContext);
6466
}
6567
}
6668
}

0 commit comments

Comments
 (0)