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

Fixes #4327 - Make sure WebApplicationServletInputStream does not block #4328

Merged
merged 1 commit into from
Dec 9, 2024
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
Expand Up @@ -122,51 +122,50 @@ public boolean isReady() {

@Override
public int read() throws IOException {
int read = -1;
int read;
if (readListener == null) {
if (finished || webApplicationRequest.getContentLength() == 0) {
return -1;
}
if (inputStream.available() > 0) {
/*
* Because the inputstream indicates we have bytes available we
* read the next byte assuming it won't block.
*/
read = inputStream.read();
} else {
/*
* Because we do not know if the underlying inputstream can
* block indefinitely we make sure we read from the inputstream
* with a timeout so we do not block the thread indefinitely.
*
* If we do not get a read to succeed within the 30 seconds
* timeout we return -1 to indicate we assume the end of the
* stream has been reached.
*/
ExecutorService executor = Executors.newSingleThreadExecutor();

Callable<Integer> readTask = () -> {
return inputStream.read();
};

Future<Integer> future = executor.submit(readTask);

try {
read = future.get(30, TimeUnit.SECONDS);
} catch (TimeoutException | InterruptedException | ExecutionException e) {
read = -1;
} finally {
executor.shutdown();
}
}
read = readWithTimeout();
index++;
if (index == webApplicationRequest.getContentLength() || read == -1) {
finished = true;
}
} else {
if (inputStream.available() > 0) {
read = inputStream.read();
}
read = readWithTimeout();
}
return read;
}

/**
* Read with a timeout.
*
* @return the byte read or -1 if the end has been reached (or a timeout
* occurred).
*/
private int readWithTimeout() {
int read = -1;
/*
* Because we do not know if the underlying inputstream can
* block indefinitely we make sure we read from the inputstream
* with a timeout so we do not block the thread indefinitely.
*
* If we do not get a read to succeed within the 30 seconds
* timeout we return -1 to indicate we assume the end of the
* stream has been reached.
*/
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<Integer> readTask = () -> {
return inputStream.read();
};
Future<Integer> future = executor.submit(readTask);
try {
read = future.get(30, TimeUnit.SECONDS);
} catch (TimeoutException | InterruptedException | ExecutionException e) {
read = -1;
} finally {
executor.shutdown();
}
return read;
}
Expand Down
Loading