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

fix the issue that makes jib stuck at reading images from local docke… #4048

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,20 @@ public DockerImageDetails inspect(ImageReference imageReference)
throws IOException, InterruptedException {
Process inspectProcess =
docker("inspect", "-f", "{{json .}}", "--type", "image", imageReference.toString());
if (inspectProcess.waitFor() != 0) {
throw new IOException(
"'docker inspect' command failed with error: " + getStderrOutput(inspectProcess));

try (InputStreamReader stdout =
new InputStreamReader(inspectProcess.getInputStream(), StandardCharsets.UTF_8)) {

// CharStreams.toString() will block until the end of stream is reached.
String output = CharStreams.toString(stdout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a concern. stdout is an input stream, and at this point, you may not have reached "EOF" of the stream. That is, the process may be still running and writing characters to the stream after this line, which you will miss onward. And hypothetically, you can run into the same issue again.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. According to the documentation and source code(https://github.com/google/guava/blob/master/guava/src/com/google/common/io/CharStreams.java) of CharStreams.toString(), it reads all data from the readable. Besides, this is how other parts of this class handles outputs from the docker process.

But of course please do suggest if you have better ideas in mind. ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a stream, and what the doc says is a different thing.

To be correct, you need to keep consuming the stream while the process is running (i.e., looping), because you never know when the process will start/resume writing data on the other side of the stream. And after you noticed the process is terminated, you need to consume the stream one last time.

However, I'd be happy just to see an improvement. So, what I suggest is to read the stream once more after process.waitFor(). It's definitely an improvement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol. I do get what you mean but allow me to elaborate a bit more and you can correct me if I am wrong. :)

image

image

Reference for the behavior of InputStream.read() can be found here:
https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read-byte:A-
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good to know. One thing to correct though: you said "r is an InputStream which is not a Reader", but it is a Reader. Can you read the JDK code further to make sure it works too?

try (InputStreamReader stdout = new InputStreamReader(...) {
      String output = CharStreams.toString(stdout);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what's the rationale for using a Reader?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. My bad. It is indeed a Reader in our case. Thanks for the correction. :)
Below is the case for Reader.
image

As you can see, it will still exhaust the underlying Stream. Reference can be found here. https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html
image

As for the rationale of using a Reader, I was merely following the style of existing code of the same class so that it is consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool then. Please just leave the comment "CharStreams.toString() will block until the end of stream is reached." That'll save people questioning the same concern as I did.

I'll let the repo maintainer to review and approve this. Ping them if necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Added the comment as requested. :)


if (inspectProcess.waitFor() != 0) {
throw new IOException(
"'docker inspect' command failed with error: " + getStderrOutput(inspectProcess));
}

return JsonTemplateMapper.readJson(output, DockerImageDetails.class);
}
return JsonTemplateMapper.readJson(inspectProcess.getInputStream(), DockerImageDetails.class);
}

/** Runs a {@code docker} command. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public void testSize_fail() throws InterruptedException {
Assert.assertEquals("inspect", subcommand.get(0));
return mockProcessBuilder;
});

Mockito.when(mockProcess.getInputStream()).thenReturn(InputStream.nullInputStream());

Mockito.when(mockProcess.waitFor()).thenReturn(1);

Mockito.when(mockProcess.getErrorStream())
Expand Down