Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ProtoOutputStream thread-safe #6547

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions p2p/src/main/java/bisq/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static int getShutdownTimeout() {
private final ConnectionStatistics connectionStatistics;

// set in init
private SynchronizedProtoOutputStream protoOutputStream;
private ProtoOutputStream protoOutputStream;

// mutable data, set from other threads but not changed internally.
@Getter
Expand Down Expand Up @@ -198,7 +198,7 @@ private void init(@Nullable NodeAddress peersNodeAddress) {
// When you construct an ObjectInputStream, in the constructor the class attempts to read a header that
// the associated ObjectOutputStream on the other end of the connection has written.
// It will not return until that header has been read.
protoOutputStream = new SynchronizedProtoOutputStream(socket.getOutputStream(), statistic);
protoOutputStream = new ProtoOutputStream(socket.getOutputStream(), statistic);
protoInputStream = socket.getInputStream();
// We create a thread for handling inputStream data
singleThreadExecutor.submit(this);
Expand Down
41 changes: 39 additions & 2 deletions p2p/src/main/java/bisq/network/p2p/network/ProtoOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,65 @@
import java.io.IOException;
import java.io.OutputStream;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.concurrent.ThreadSafe;

@NotThreadSafe
@ThreadSafe
class ProtoOutputStream {
private static final Logger log = LoggerFactory.getLogger(ProtoOutputStream.class);

private final OutputStream outputStream;
private final Statistic statistic;

private final AtomicBoolean isConnectionActive = new AtomicBoolean(true);
private final Lock lock = new ReentrantLock();

ProtoOutputStream(OutputStream outputStream, Statistic statistic) {
this.outputStream = outputStream;
this.statistic = statistic;
}

void writeEnvelope(NetworkEnvelope envelope) {
lock.lock();

try {
writeEnvelopeOrThrow(envelope);
} catch (IOException e) {
if (!isConnectionActive.get()) {
// Connection was closed by us.
return;
}

log.error("Failed to write envelope", e);
throw new BisqRuntimeException("Failed to write envelope", e);

} finally {
lock.unlock();
}
}

void onConnectionShutdown() {
isConnectionActive.set(false);

boolean acquiredLock = tryToAcquireLock();
if (!acquiredLock) {
return;
}

try {
outputStream.close();
} catch (Throwable t) {
log.error("Failed to close connection", t);

} finally {
lock.unlock();
}
}

Expand All @@ -74,4 +102,13 @@ private void writeEnvelopeOrThrow(NetworkEnvelope envelope) throws IOException {
statistic.updateLastActivityTimestamp();
}
}

private boolean tryToAcquireLock() {
long shutdownTimeout = Connection.getShutdownTimeout();
try {
return lock.tryLock(shutdownTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
return false;
}
}
}

This file was deleted.