Skip to content

Commit

Permalink
Merge pull request docker#6882 from ulyssessouza/fix_attach_restartin…
Browse files Browse the repository at this point in the history
…g_container

Fix race condition on watch_events
  • Loading branch information
ulyssessouza committed Sep 5, 2019
2 parents 9973f05 + 47d170b commit ecf03fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compose/cli/log_printer.py
Expand Up @@ -230,7 +230,13 @@ def watch_events(thread_map, event_stream, presenters, thread_args):

# Container crashed so we should reattach to it
if event['id'] in crashed_containers:
event['container'].attach_log_stream()
container = event['container']
if not container.is_restarting:
try:
container.attach_log_stream()
except APIError:
# Just ignore errors when reattaching to already crashed containers
pass
crashed_containers.remove(event['id'])

thread_map[event['id']] = build_thread(
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/cli/log_printer_test.py
Expand Up @@ -152,6 +152,17 @@ def test_start_event(self, thread_map, mock_presenters):
*thread_args)
assert container_id in thread_map

def test_container_attach_event(self, thread_map, mock_presenters):
container_id = 'abcd'
mock_container = mock.Mock(is_restarting=False)
mock_container.attach_log_stream.side_effect = APIError("race condition")
event_die = {'action': 'die', 'id': container_id}
event_start = {'action': 'start', 'id': container_id, 'container': mock_container}
event_stream = [event_die, event_start]
thread_args = 'foo', 'bar'
watch_events(thread_map, event_stream, mock_presenters, thread_args)
assert mock_container.attach_log_stream.called

def test_other_event(self, thread_map, mock_presenters):
container_id = 'abcd'
event_stream = [{'action': 'create', 'id': container_id}]
Expand Down

0 comments on commit ecf03fe

Please sign in to comment.