davidcaron opened a new pull request #13793:
URL: https://github.com/apache/airflow/pull/13793
Iterable values in `cli.attach(..., stream=True)` are not necessarily single
lines.
Lines can be merged together or split because of buffering.
When this split happens just before the last newline character, the returned
value of `execute` is an empty byte string.
Also, fix the return type annotation (the return type is not changed, only
the annotation).
I used these files to recreate the issue:
`Dockerfile`
```dockerfile
FROM python:3.8-slim-buster
WORKDIR /code
ADD main.py .
CMD [ "python", "main.py" ]
```
`main.py`
```python
from random import randint
for _ in range(randint(1, 100)):
print(f"output")
print("last_line")
```
`script.py`
```python
from docker import APIClient
cli = APIClient()
def run_image():
container = cli.create_container(
command="python main.py",
host_config=cli.create_host_config(auto_remove=False),
image="docker-bug",
tty=True,
)
lines = cli.attach(container=container["Id"], stdout=True, stderr=True,
stream=True)
cli.start(container["Id"])
line = list(lines)[-1]
cli.wait(container["Id"])
print("--- LAST LINES ---")
print(repr(line))
print(repr(cli.logs(container=container["Id"], tail=1).strip()))
cli.remove_container(container["Id"])
if __name__ == "__main__":
for i in range(1000):
run_image()
```
When running:
```bash
$ docker build -t docker-bug .
$ python script.py
```
The output will look something like:
```
--- LAST LINES ---
b'output\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\nlast_line\r\n'
b'last_line'
--- LAST LINES ---
b'output\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\noutput\r\nlast_line\r\n'
b'last_line'
--- LAST LINES ---
b'output\r\noutput\r\nlast_line\r\n'
b'last_line'
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]