Skip to content

Commit

Permalink
Make sure the socket is always cleaned up
Browse files Browse the repository at this point in the history
Cleanup according to the style used for BindState.
  • Loading branch information
rmaucher committed Jan 8, 2021
1 parent d6d3259 commit 0738909
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
21 changes: 15 additions & 6 deletions java/org/apache/tomcat/util/net/AbstractEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,26 @@ public SocketState process(SocketWrapperBase<S> socket,
}

protected enum BindState {
UNBOUND(false),
BOUND_ON_INIT(true),
BOUND_ON_START(true),
SOCKET_CLOSED_ON_STOP(false);
UNBOUND(false, false),
BOUND_ON_INIT(true, true),
BOUND_ON_START(true, true),
SOCKET_CLOSED_ON_STOP(false, true);

private final boolean bound;
private final boolean wasBound;

private BindState(boolean bound) {
private BindState(boolean bound, boolean wasBound) {
this.bound = bound;
this.wasBound = wasBound;
}

public boolean isBound() {
return bound;
}

public boolean wasBound() {
return wasBound;
}
}


Expand Down Expand Up @@ -617,7 +623,10 @@ public final int getLocalPort() {
private boolean bindOnInit = true;
public boolean getBindOnInit() { return bindOnInit; }
public void setBindOnInit(boolean b) { this.bindOnInit = b; }
protected volatile BindState bindState = BindState.UNBOUND;
private volatile BindState bindState = BindState.UNBOUND;
protected BindState getBindState() {
return bindState;
}

/**
* Keepalive timeout, if not set the soTimeout is used.
Expand Down
17 changes: 10 additions & 7 deletions java/org/apache/tomcat/util/net/NioEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,16 @@ public void unbind() throws Exception {

@Override
protected void doCloseServerSocket() throws IOException {
if (!getUseInheritedChannel() && serverSock != null) {
// Close server socket
serverSock.close();
}
serverSock = null;
if (getUnixDomainSocketPath() != null && bindState != BindState.UNBOUND) {
Files.delete(Paths.get(getUnixDomainSocketPath()));
try {
if (!getUseInheritedChannel() && serverSock != null) {
// Close server socket
serverSock.close();
}
serverSock = null;
} finally {
if (getUnixDomainSocketPath() != null && getBindState().wasBound()) {
Files.delete(Paths.get(getUnixDomainSocketPath()));
}
}
}

Expand Down

0 comments on commit 0738909

Please sign in to comment.