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

[7.x] Synchronize processInStream.close() call #50581

Merged
merged 2 commits into from
Jan 3, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception
"Finished analysis");
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/49680")
public void testStopAndRestart() throws Exception {
initialize("regression_stop_and_restart");
indexData(sourceIndex, 350, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

/**
Expand All @@ -44,6 +45,9 @@ public abstract class AbstractNativeProcess implements NativeProcess {
private final String jobId;
private final CppLogMessageHandler cppLogHandler;
private final OutputStream processInStream;
// We need this as in Java 8 closing {@link FilterOutputStream} is not idempotent (i.e. cannot be performed twice).
// For more details regarding the underlying issue see https://bugs.openjdk.java.net/browse/JDK-8054565
private final AtomicBoolean processInStreamClosed = new AtomicBoolean();
private final InputStream processOutStream;
private final OutputStream processRestoreStream;
private final LengthEncodedWriter recordWriter;
Expand Down Expand Up @@ -163,7 +167,10 @@ public void close() throws IOException {
processCloseInitiated = true;
// closing its input causes the process to exit
if (processInStream != null) {
processInStream.close();
// Make sure {@code processInStream.close()} is called at most once.
if (processInStreamClosed.compareAndSet(false, true)) {
processInStream.close();
}
}
// wait for the process to exit by waiting for end-of-file on the named pipe connected
// to the state processor - it may take a long time for all the model state to be
Expand Down Expand Up @@ -209,7 +216,10 @@ public void kill() throws IOException {
} finally {
try {
if (processInStream != null) {
processInStream.close();
// Make sure {@code processInStream.close()} is called at most once.
if (processInStreamClosed.compareAndSet(false, true)) {
processInStream.close();
}
}
} catch (IOException e) {
// Ignore it - we're shutting down and the method itself has logged a warning
Expand Down