Skip to content

Commit

Permalink
Possible deadlock in shutdown. 0.9.5 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Nov 16, 2023
1 parent 1bd16a8 commit 6a31e03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sshtools</groupId>
<artifactId>uhttpd</artifactId>
<version>0.9.5-SNAPSHOT</version>
<version>0.9.5</version>
<name>µTTPD - A very small HTTP/HTTPS server</name>
<properties>
<maven.compiler.target>11</maven.compiler.target>
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/com/sshtools/uhttpd/UHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -101,6 +102,7 @@
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -322,14 +324,13 @@ public final void run() {
do {
wireProtocol.transact();
times++;
} while (times < rootContext.keepAliveMax);
} while (!closed && times < rootContext.keepAliveMax);
} catch (ClosedChannelException | EOFException e) {
LOG.log(Level.TRACE, "EOF.", e);
} catch(SocketTimeoutException ste) {
LOG.log(Level.TRACE, "Timeout.", ste);
} catch (Exception e) {
LOG.log(Level.ERROR, "Failed handling connection.", e);
e.printStackTrace();// argh
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -1107,17 +1108,20 @@ public static class Session {
}

public static Session get() {
return get(true).orElseThrow();
}

public static Optional<Session> get(boolean create) {
var session = current.get();
if(session == null) {
if(session == null && create) {
session = new Session();
current.set(session);
var tx = Transaction.get();
if(tx.responded()) {
throw new IllegalStateException("A session cookie must be created, but the response has already been committed.");
}
return session;
}
return session;
return Optional.ofNullable(session);
}

@Override
Expand Down Expand Up @@ -1538,7 +1542,7 @@ String text() {

/**
* Select a {@link Handler} based on its {@link Transaction#path()}, i.e. URI.
* If the URI matches, the handler will be executred.
* If the URI matches, the handler will be executed.
*
*/
public static final class RegularExpressionSelector implements HandlerSelector {
Expand All @@ -1552,7 +1556,13 @@ public RegularExpressionSelector(String regexp) {
@Override
public boolean matches(Transaction req) {
var path = req.path().toString();
return pattern.matcher(path).matches();
var matcher = pattern.matcher(path);
if(matcher.matches()) {
for(int i = 1 ; i <= matcher.groupCount(); i++) {
req.matches.add(matcher.group(i));
}
}
return matcher.matches();
}

}
Expand Down Expand Up @@ -2209,6 +2219,11 @@ public final Optional<String> queryString() {
return queryString;
}


public final Transaction redirect(Status status, Path location) {
return redirect(status, location.toString());
}

public final Transaction redirect(Status status, String location) {
checkNotResponded();
if (status != Status.MOVED_PERMANENTLY && status != Status.MOVED_TEMPORARILY)
Expand Down Expand Up @@ -4865,7 +4880,7 @@ private final static class RootContextImpl extends AbstractContext implements Ro
private final ServerSocket sslServerSocket;
private final String threadName;
private final boolean daemon;
private final Set<Client> clients = Collections.synchronizedSet(new HashSet<>());
private final Set<Client> clients = new CopyOnWriteArraySet<>();

private Thread otherThread;
private Thread serverThread;
Expand Down Expand Up @@ -4985,9 +5000,9 @@ protected void onClose() {
if (sslServerSocket != null)
sslServerSocket.close();
} finally {
for(var c : new HashSet<>(clients)) {
while(!clients.isEmpty()) {
try {
c.close();
clients.iterator().next().close();
}
catch(Exception e) {}
}
Expand Down

0 comments on commit 6a31e03

Please sign in to comment.