Skip to content

Commit

Permalink
Merge branch 'bugfixes/16-EINVAL-during-bus-transition'
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Mar 20, 2021
2 parents 41c0292 + 282cf75 commit f13d5ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
23 changes: 21 additions & 2 deletions magicbus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
are free to define their own channels. If a message is sent to a
channel that has not been defined or has no listeners, there is no effect.
"""
import errno
import os
import random
try:
Expand Down Expand Up @@ -202,7 +203,23 @@ def _transition(self, newstate, *args, **kwargs):
# Write to any pipes created by threads calling self.wait().
# Use list() to avoid "Set changed size during iteration" errors.
for read_fd, write_fd in list(self._state_transition_pipes):
os.write(write_fd, b'1')
try:
os.write(write_fd, b'1')
except OSError as os_err_exc:
# Ref: https://github.com/cherrypy/magicbus/issues/16
#
# NOTE: This is supposedly happening when `self.wait()`
# NOTE: closes the pipe on disposal.
if os_err_exc.errno not in {
# NOTE: This has been noticed in CI under both
# NOTE: Ubuntu and Windows:
errno.EBADF,

# NOTE: This behavior is currently only observed
# NOTE: under Windows in GHA CI/CD workflows:
errno.EINVAL,
}:
raise

# Note: logging here means 1) the initial transition
# will not be logged if loggers are set up in the initial
Expand Down Expand Up @@ -335,8 +352,10 @@ def _wait():
self.publish(channel)
finally:
self._state_transition_pipes.discard(pipe)
os.close(read_fd)
# NOTE: Closing the write file descriptor first
# NOTE: to prevent "Broken pipe" in `self._transition()`.
os.close(write_fd)
os.close(read_fd)

# From http://psyco.sourceforge.net/psycoguide/bugs.html:
# "The compiled machine code does not include the regular polling
Expand Down
11 changes: 0 additions & 11 deletions magicbus/test/test_processbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,6 @@ def test_idle_to_exit(self):
'Bus state: EXITED'
])

@pytest.mark.xfail(
reason=r"""Fails intermittently with
Traceback (most recent call last):
File "D:\a\magicbus\magicbus\magicbus\base.py",
line 205, in _transition
os.write(write_fd, b'1')
OSError: [Errno 22] Invalid argument
""",
raises=AssertionError,
strict=False, # Because it's flaky
)
def test_wait(self):
b = ProcessBus()
self.log(b)
Expand Down

0 comments on commit f13d5ae

Please sign in to comment.