Skip to content

Commit 2758d1a

Browse files
fix: Fix Logic Error in Container Creation Leading to Test Case Failure
1 parent 94ffa88 commit 2758d1a

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class DockerService implements DisposableBean {
3838
private final String jshellWrapperBaseImageName;
3939

4040
public DockerService(Config config, StartupScriptsService startupScriptsService)
41-
throws InterruptedException, IOException {
41+
throws InterruptedException {
4242
this.startupScriptsService = startupScriptsService;
4343
DefaultDockerClientConfig clientConfig =
4444
DefaultDockerClientConfig.createDefaultConfigBuilder().build();
@@ -143,15 +143,16 @@ public String createContainer(String name) {
143143
*/
144144
public ContainerState initializeContainer(String name, StartupScriptId startupScriptId)
145145
throws IOException {
146-
if (startupScriptId == null || cachedContainers.isEmpty() || !cachedContainers.containsKey(startupScriptId)) {
146+
if (startupScriptId == null || cachedContainers.isEmpty()
147+
|| !cachedContainers.containsKey(startupScriptId)) {
147148
String containerId = createContainer(name);
148-
return setupContainerWithScript(containerId, true, startupScriptId);
149+
return setupContainerWithScript(containerId, false, startupScriptId);
149150
}
150151
String containerId = cachedContainers.get(startupScriptId);
151152
executor.submit(() -> initializeCachedContainer(startupScriptId));
152153

153154
client.renameContainerCmd(containerId).withName(name).exec();
154-
return setupContainerWithScript(containerId, false, startupScriptId);
155+
return setupContainerWithScript(containerId, true, startupScriptId);
155156
}
156157

157158
/**
@@ -167,11 +168,12 @@ private void initializeCachedContainer(StartupScriptId startupScriptId) {
167168
try (PipedInputStream containerInput = new PipedInputStream();
168169
BufferedWriter writer = new BufferedWriter(
169170
new OutputStreamWriter(new PipedOutputStream(containerInput)))) {
170-
attachToContainer(id, containerInput);
171+
InputStream containerOutput = attachToContainer(id, containerInput, true);
171172

172173
writer.write(Utils.sanitizeStartupScript(startupScriptsService.get(startupScriptId)));
173174
writer.newLine();
174175
writer.flush();
176+
containerOutput.close();
175177

176178
cachedContainers.put(startupScriptId, id);
177179
} catch (IOException e) {
@@ -197,7 +199,7 @@ private ContainerState setupContainerWithScript(String containerId, boolean isCa
197199
BufferedWriter writer =
198200
new BufferedWriter(new OutputStreamWriter(new PipedOutputStream(containerInput)));
199201

200-
InputStream containerOutput = attachToContainer(containerId, containerInput);
202+
InputStream containerOutput = attachToContainer(containerId, containerInput, false);
201203
BufferedReader reader = new BufferedReader(new InputStreamReader(containerOutput));
202204

203205
if (!isCached) {
@@ -224,13 +226,14 @@ public void startContainer(String containerId) {
224226
* Attaches to a running Docker container's input (stdin) and output streams (stdout, stderr).
225227
* Logs any output from stderr and returns an InputStream to read stdout.
226228
*
227-
* @param containerId the ID of the running container to attach to
228-
* @param containerInput the input stream (containerInput) to send to the container
229+
* @param containerId The ID of the running container to attach to.
230+
* @param containerInput The input stream (containerInput) to send to the container.
231+
* @param isCached Indicator if the container is cached to prevent writing to output stream.
229232
* @return InputStream to read the container's stdout
230233
* @throws IOException if an I/O error occurs
231234
*/
232-
public InputStream attachToContainer(String containerId, InputStream containerInput)
233-
throws IOException {
235+
public InputStream attachToContainer(String containerId, InputStream containerInput,
236+
boolean isCached) throws IOException {
234237
PipedInputStream pipeIn = new PipedInputStream();
235238
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
236239

@@ -247,7 +250,9 @@ public void onNext(Frame object) {
247250
String payloadString =
248251
new String(object.getPayload(), StandardCharsets.UTF_8);
249252
if (object.getStreamType() == StreamType.STDOUT) {
250-
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
253+
if (!isCached) {
254+
pipeOut.write(object.getPayload()); // Write stdout data to pipeOut
255+
}
251256
} else {
252257
LOGGER.warn("Received STDERR from container {}: {}", containerId,
253258
payloadString);

0 commit comments

Comments
 (0)