mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): stop closing a Docker stream from reading the rest of its body (#9339)
Closing a stream from a Docker GET read the rest of its body, since httpclient5 drains a response on close to keep its connection, so a caller that stopped after the first bytes of a large body still waited for all of it. On close the stream now reads at most 64 KiB of what is left: a body that ends there keeps its pooled connection, and a longer one is aborted. Closing it twice does nothing more. Refs #9244
This commit is contained in:
1 parent
f5849990e4
commit
6018a41837
2 files changed
+218
-1
No files matched your search
@@ -36,6 +36,14 @@ final class FlociInvocationBuilder implements InvocationBuilder {
|
||||
/** docker-java stream callbacks running now, each on its own virtual thread. */
|
||||
private static final AtomicInteger ACTIVE_STREAMS = new AtomicInteger();
|
||||
|
||||
/**
|
||||
* How much of a {@link #get()} body left unread at close is read to keep its pooled connection.
|
||||
* Past this the call is aborted instead: httpclient5 reads a body to its end on close, and an
|
||||
* archive closed after its first entry, as the Lambda working directory check does, would
|
||||
* otherwise download the whole directory.
|
||||
*/
|
||||
static final long DRAIN_ON_CLOSE_LIMIT = 64 * 1024;
|
||||
|
||||
private final DockerHttpClient.Request.Builder requestBuilder;
|
||||
private final DockerHttpClient dockerHttpClient;
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -211,17 +219,58 @@ final class FlociInvocationBuilder implements InvocationBuilder {
|
||||
|
||||
DockerHttpClient.Response response = execute(request);
|
||||
return new FilterInputStream(response.getBody()) {
|
||||
private boolean closed;
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
closed = true;
|
||||
try {
|
||||
super.close();
|
||||
if (endsWithin(in, DRAIN_ON_CLOSE_LIMIT)) {
|
||||
super.close();
|
||||
} else {
|
||||
// Abort first: closing the body would read the rest of it to keep the connection.
|
||||
response.close();
|
||||
closeAbandoned();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void closeAbandoned() {
|
||||
try {
|
||||
super.close();
|
||||
} catch (IOException expected) {
|
||||
// The aborted connection is gone, and the body was not wanted.
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code stream} ends within {@code limit} more bytes. Reads (and discards) them, so a
|
||||
* body that ends here gives its connection back to the pool; a read that fails counts as not ending.
|
||||
*/
|
||||
static boolean endsWithin(InputStream stream, long limit) {
|
||||
byte[] buffer = new byte[8192];
|
||||
long read = 0;
|
||||
try {
|
||||
while (read <= limit) {
|
||||
int n = stream.read(buffer);
|
||||
if (n == -1) {
|
||||
return true;
|
||||
}
|
||||
read += n;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(InputStream body, MediaType mediaType) {
|
||||
DockerHttpClient.Request request = requestBuilder
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package io.floci.aws.core.common.docker;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.testing.ParallelLocks;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.api.parallel.ResourceAccessMode;
|
||||
import org.junit.jupiter.api.parallel.ResourceLock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.URI;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
|
||||
/** How a stream from {@link FlociInvocationBuilder#get()} gives its connection back when it is closed. */
|
||||
@ResourceLock(value = ParallelLocks.PORTS, mode = ResourceAccessMode.READ)
|
||||
class FlociInvocationBuilderTest {
|
||||
|
||||
// Catches: closing an archive stream after its first entry reading the rest of the response
|
||||
// first, which made a Lambda image's working directory check download all of /var on each
|
||||
// cold start (#9244). The server here never ends the body, so that close never returns.
|
||||
@Test
|
||||
@Timeout(60)
|
||||
void closingAStreamPartWayThroughDoesNotReadTheRestOfTheResponse(@TempDir Path dir) throws Exception {
|
||||
try (DockerStub docker = new DockerStub(dir.resolve("docker.sock"));
|
||||
FlociDockerHttpClient client = client(docker)) {
|
||||
InputStream archive = get(client, "/containers/abc/archive?path=/var");
|
||||
assertNotEquals(-1, archive.read(new byte[512]), "the first tar header should arrive");
|
||||
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(10), archive::close,
|
||||
"closing the stream should not wait for the rest of the archive");
|
||||
}
|
||||
}
|
||||
|
||||
// Catches: an abort on every close, which would give up a connection the pool could keep: a
|
||||
// stream read to its end, and docker-java's ping, which closes the daemon's "OK" unread.
|
||||
@Test
|
||||
@Timeout(60)
|
||||
void aStreamThatEndsSoonAfterItIsClosedKeepsItsPooledConnection(@TempDir Path dir) throws Exception {
|
||||
try (DockerStub docker = new DockerStub(dir.resolve("docker.sock"));
|
||||
FlociDockerHttpClient client = client(docker)) {
|
||||
try (InputStream small = get(client, "/containers/abc/archive?path=/srv")) {
|
||||
assertEquals(DockerStub.SMALL_ARCHIVE_BYTES, small.readAllBytes().length);
|
||||
}
|
||||
get(client, "/containers/abc/archive?path=/srv").close();
|
||||
get(client, "/_ping").close();
|
||||
get(client, "/_ping").close();
|
||||
|
||||
assertEquals(1, docker.connections.get(), "four calls should share one connection");
|
||||
}
|
||||
}
|
||||
|
||||
private static FlociDockerHttpClient client(DockerStub docker) {
|
||||
return new FlociDockerHttpClient.Builder()
|
||||
.dockerHost(URI.create("unix://" + docker.path))
|
||||
.responseTimeout(Duration.ofSeconds(5))
|
||||
.build();
|
||||
}
|
||||
|
||||
private static InputStream get(FlociDockerHttpClient client, String path) {
|
||||
return new FlociInvocationBuilder(client, new ObjectMapper(), path).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* A keep-alive daemon. An archive of {@code /var} is a chunked body that never ends; an archive of
|
||||
* {@code /srv} is a short chunked body; anything else is a 200 "OK".
|
||||
*/
|
||||
private static final class DockerStub implements AutoCloseable {
|
||||
|
||||
static final int SMALL_ARCHIVE_BYTES = 4 * 1024;
|
||||
|
||||
private final Path path;
|
||||
private final ServerSocketChannel server;
|
||||
private final AtomicInteger connections = new AtomicInteger();
|
||||
private final List<SocketChannel> channels = new CopyOnWriteArrayList<>();
|
||||
|
||||
DockerStub(Path path) throws IOException {
|
||||
this.path = path;
|
||||
this.server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
server.bind(UnixDomainSocketAddress.of(path));
|
||||
Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
while (true) {
|
||||
SocketChannel channel = server.accept();
|
||||
connections.incrementAndGet();
|
||||
channels.add(channel);
|
||||
Thread.ofVirtual().start(() -> serve(channel));
|
||||
}
|
||||
} catch (IOException expected) {
|
||||
// The server channel closed at the end of the test.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void serve(SocketChannel channel) {
|
||||
try {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(4096);
|
||||
StringBuilder pending = new StringBuilder();
|
||||
while (channel.read(buffer) != -1) {
|
||||
buffer.flip();
|
||||
pending.append(StandardCharsets.US_ASCII.decode(buffer));
|
||||
buffer.clear();
|
||||
int end;
|
||||
while ((end = pending.indexOf("\r\n\r\n")) >= 0) {
|
||||
String requestLine = pending.substring(0, pending.indexOf("\r\n"));
|
||||
pending.delete(0, end + 4);
|
||||
if (requestLine.contains("path=/var")) {
|
||||
writeChunked(channel, Long.MAX_VALUE);
|
||||
} else if (requestLine.contains("path=/srv")) {
|
||||
writeChunked(channel, SMALL_ARCHIVE_BYTES);
|
||||
} else {
|
||||
write(channel, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 2\r\n\r\nOK");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException expected) {
|
||||
// The client or the test closed the connection.
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeChunked(SocketChannel channel, long bytes) throws IOException {
|
||||
write(channel, "HTTP/1.1 200 OK\r\nContent-Type: application/x-tar\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
byte[] chunk = new byte[1024];
|
||||
Arrays.fill(chunk, (byte) 'x');
|
||||
String size = Integer.toHexString(chunk.length) + "\r\n";
|
||||
for (long sent = 0; sent < bytes; sent += chunk.length) {
|
||||
write(channel, size);
|
||||
ByteBuffer data = ByteBuffer.wrap(chunk);
|
||||
while (data.hasRemaining()) {
|
||||
channel.write(data);
|
||||
}
|
||||
write(channel, "\r\n");
|
||||
}
|
||||
write(channel, "0\r\n\r\n");
|
||||
}
|
||||
|
||||
private static void write(SocketChannel channel, String text) throws IOException {
|
||||
ByteBuffer bytes = ByteBuffer.wrap(text.getBytes(StandardCharsets.US_ASCII));
|
||||
while (bytes.hasRemaining()) {
|
||||
channel.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
server.close();
|
||||
for (SocketChannel channel : channels) {
|
||||
channel.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user