Skip to content

Commit fa5aa3c

Browse files
committed
Flush wrapped stdout/stderr OutputStream after every write
Fixes #379
1 parent 7cb38ae commit fa5aa3c

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PosixResources.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -43,11 +43,13 @@
4343
import java.io.IOException;
4444
import java.io.InputStream;
4545
import java.io.OutputStream;
46+
import java.nio.ByteBuffer;
4647
import java.nio.channels.Channel;
4748
import java.nio.channels.Channels;
4849
import java.nio.channels.FileLock;
4950
import java.nio.channels.Pipe;
5051
import java.nio.channels.SeekableByteChannel;
52+
import java.nio.channels.WritableByteChannel;
5153
import java.util.ArrayList;
5254
import java.util.Collections;
5355
import java.util.HashMap;
@@ -154,6 +156,33 @@ public Process destroyForcibly() {
154156
}
155157
}
156158

159+
private static class FlushingWritableByteChannel implements WritableByteChannel {
160+
private final OutputStream stream;
161+
private final WritableByteChannel delegate;
162+
163+
private FlushingWritableByteChannel(OutputStream stream) {
164+
this.stream = stream;
165+
delegate = Channels.newChannel(stream);
166+
}
167+
168+
@Override
169+
public int write(ByteBuffer src) throws IOException {
170+
int res = delegate.write(src);
171+
stream.flush();
172+
return res;
173+
}
174+
175+
@Override
176+
public boolean isOpen() {
177+
return delegate.isOpen();
178+
}
179+
180+
@Override
181+
public void close() throws IOException {
182+
delegate.close();
183+
}
184+
}
185+
157186
private static class ChannelWrapper {
158187
Channel channel;
159188
int cnt;
@@ -184,7 +213,7 @@ void setNewChannel(InputStream inputStream) {
184213
}
185214

186215
void setNewChannel(OutputStream outputStream) {
187-
this.channel = Channels.newChannel(outputStream);
216+
this.channel = new FlushingWritableByteChannel(outputStream);
188217
this.cnt = 1;
189218
}
190219
}

0 commit comments

Comments
 (0)