Skip to content

Commit 069e01b

Browse files
committed
Gracefull shutdown on stop()
This should solve the issue TooTallNate#620
1 parent 5cc8e66 commit 069e01b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ public void stop( int timeout ) throws InterruptedException {
253253
wsf.close();
254254

255255
synchronized ( this ) {
256-
if( selectorthread != null && selectorthread != Thread.currentThread() ) {
257-
selectorthread.interrupt();
256+
if( selectorthread != null ) {
258257
selector.wakeup();
259258
selectorthread.join( timeout );
260259
}
@@ -329,11 +328,19 @@ public void run() {
329328
return;
330329
}
331330
try {
332-
while ( !selectorthread.isInterrupted() ) {
331+
int iShutdownCount = 5;
332+
int selectTimeout = 0;
333+
while ( !selectorthread.isInterrupted() && iShutdownCount != 0) {
333334
SelectionKey key = null;
334335
WebSocketImpl conn = null;
335336
try {
336-
selector.select();
337+
if (isclosed.get()) {
338+
selectTimeout = 5;
339+
}
340+
int keyCount = selector.select( selectTimeout );
341+
if (keyCount == 0 && isclosed.get()) {
342+
iShutdownCount--;
343+
}
337344
Set<SelectionKey> keys = selector.selectedKeys();
338345
Iterator<SelectionKey> i = keys.iterator();
339346

0 commit comments

Comments
 (0)