Skip to content

Commit

Permalink
Fix ISOServer shutdown thread deadlock
Browse files Browse the repository at this point in the history
closes #631
  • Loading branch information
ar committed Jan 5, 2025
1 parent 6b98e1a commit a17bafd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 10 additions & 3 deletions jpos/src/main/java/org/jpos/iso/ISOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -97,6 +96,7 @@ private enum PermLogPolicy {
private int permitsCount = DEFAULT_MAX_SESSIONS;
private static final long SMALL_RELAX = 250;
private static final long LONG_RELAX = 5000;
private static final long SHUTDOWN_WAIT = 15000;
private final UUID uuid = UUID.randomUUID();

/**
Expand Down Expand Up @@ -232,15 +232,22 @@ public void shutdown () {
shutdownChannels();
}
});
executor.shutdown();
try {
if (!executor.awaitTermination(SHUTDOWN_WAIT, TimeUnit.MILLISECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void shutdownServer () {
try {
if (serverSocket != null) {
serverSocket.close ();
fireEvent(new ISOServerShutdownEvent(this));
}
executor.awaitTermination(LONG_RELAX, TimeUnit.SECONDS);
} catch (IOException | InterruptedException e) {
} catch (IOException e) {
fireEvent(new ISOServerShutdownEvent(this));
Logger.log (new LogEvent (this, "shutdown", e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
import org.jpos.iso.packager.XMLPackager;
import org.jpos.util.Logger;
import org.jpos.util.SimpleLogListener;
import org.jpos.util.ThreadPool;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -84,7 +82,7 @@ public void serverSideDisconnect() throws Exception {

isoServer.shutdown();

assertThrows(SocketTimeoutException.class, () -> {
assertThrows(EOFException.class, () -> {
clientChannel.receive();
}, "clientChannel should be closed");
}
Expand Down

0 comments on commit a17bafd

Please sign in to comment.