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 output stream failures (Windows) #4375

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions conans/client/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ def write(self, data, front=None, back=None, newline=False):
data = decode_text(data) # Keep python 2 compatibility

if self._color and (front or back):
color = "%s%s" % (front or '', back or '')
end = (Style.RESET_ALL + "\n") if newline else Style.RESET_ALL # @UndefinedVariable
data = "%s%s%s" % (color, data, end)
else:
if newline:
data = "%s\n" % data

try:
self._stream.write(data)
except UnicodeError:
data = data.encode("utf8").decode("ascii", "ignore")
self._stream.write(data)
data = "%s%s%s%s" % (front or '', back or '', data, Style.RESET_ALL)
if newline:
data = "%s\n" % data

# https://github.com/conan-io/conan/issues/4277
# Windows output locks produce IOErrors
for _ in range(3):
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, this might work and fix the issue, but at least we need some kind of disclaimer for this block of code, maybe a link to the issue, an explanation, (I suppose that a test is impossible)... otherwise anyone having a look at this hack may delete it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jgsogo may be it's possible to mock self._stream.write in test, so it throws an error 3 times?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added comments and a test patching the stream.write.

try:
self._stream.write(data)
break
except IOError:
import time
time.sleep(0.02)
except UnicodeError:
data = data.encode("utf8").decode("ascii", "ignore")

self._stream.flush()

def info(self, data):
Expand Down
30 changes: 30 additions & 0 deletions conans/test/unittests/client/conan_output_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding=utf-8

import types
import unittest

from six import StringIO

from conans.client.output import ConanOutput
from mock import mock


class ConanOutputTest(unittest.TestCase):

def test_blocked_output(self):
# https://github.com/conan-io/conan/issues/4277
stream = StringIO()

def write_raise(self, data):
write_raise.counter = getattr(write_raise, "counter", 0) + 1
if write_raise.counter < 2:
raise IOError("Stdout locked")
self.super_write(data)
stream.super_write = stream.write
stream.write = types.MethodType(write_raise, stream)
out = ConanOutput(stream)

with mock.patch("time.sleep") as sleep:
out.write("Hello world")
sleep.assert_any_call(0.02)
self.assertEqual("Hello world", stream.getvalue())
13 changes: 3 additions & 10 deletions conans/util/progress_bar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
from contextlib import contextmanager

Expand All @@ -18,17 +17,15 @@ class _FileReaderWithProgressBar(object):

def __init__(self, fileobj, output, desc=None):
pb_kwargs = self.tqdm_defaults.copy()
self._ori_output = output

# If there is no terminal, just print a beat every TIMEOUT_BEAT seconds.
if not output.is_terminal:
output = _NoTerminalOutput(output)
pb_kwargs['mininterval'] = TIMEOUT_BEAT_SECONDS

self._output = output
self._fileobj = fileobj
self.seek(0, os.SEEK_END)
self._pb = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
self._tqdm_bar = tqdm(total=self.tell(), desc=desc, file=output, **pb_kwargs)
self.seek(0)

def seekable(self):
Expand All @@ -43,15 +40,11 @@ def tell(self):
def read(self, size):
prev = self.tell()
ret = self._fileobj.read(size)
self._pb.update(self.tell() - prev)
self._tqdm_bar.update(self.tell() - prev)
return ret

def pb_close(self):
self._pb.close()

def pb_write(self, message):
jgsogo marked this conversation as resolved.
Show resolved Hide resolved
""" Allow to write messages to output without interfering with the progress bar """
tqdm.write(message, file=self._ori_output)
self._tqdm_bar.close()


class _NoTerminalOutput(object):
Expand Down