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

Prevent race condition in ChunkedOutput #4571

Merged
merged 1 commit into from
Sep 23, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -53,8 +53,11 @@ public class ChunkedOutput<T> extends GenericType<T> implements Closeable {
private final BlockingDeque<T> queue = new LinkedBlockingDeque<>();
private final byte[] chunkDelimiter;
private final AtomicBoolean resumed = new AtomicBoolean(false);
private final Object lock = new Object();

// the following flushing and touchingEntityStream variables are used in a synchronized block exclusively
private boolean flushing = false;
private boolean touchingEntityStream = false;

private volatile boolean closed = false;

Expand Down Expand Up @@ -198,7 +201,7 @@ public Void call() throws IOException {
boolean shouldClose;
T t;

synchronized (ChunkedOutput.this) {
synchronized (lock) {
if (flushing) {
// if another thread is already flushing the queue, we don't have to do anything
return null;
Expand All @@ -220,6 +223,10 @@ public Void call() throws IOException {

while (t != null) {
try {
synchronized (lock) {
touchingEntityStream = true;
}

final OutputStream origStream = responseContext.getEntityStream();
final OutputStream writtenStream = requestContext.getWorkers().writeTo(
t,
Expand Down Expand Up @@ -256,10 +263,15 @@ public Void call() throws IOException {
connectionCallback.onDisconnect(asyncContext);
}
throw mpe;
} finally {
synchronized (lock) {
touchingEntityStream = false;
}
}

t = queue.poll();
if (t == null) {
synchronized (ChunkedOutput.this) {
synchronized (lock) {
// queue seems empty
// check again in the synchronized block before clearing the flushing flag
// first remember the closed flag (this has to be before polling the queue,
Expand Down Expand Up @@ -290,7 +302,11 @@ public Void call() throws IOException {
} finally {
if (closed) {
try {
responseContext.close();
synchronized (lock) {
if (!touchingEntityStream) {
responseContext.close();
} // else the next thread will close responseContext
}
} catch (final Exception e) {
// if no exception remembered before, remember this one
// otherwise the previously remembered exception (from catch clause) takes precedence
Expand Down