Skip to content

Commit 74834c8

Browse files
committed
Merge remote-tracking branch 'origin/dmw'
* origin/dmw: issue #605: update Changelog. issue #605: ansible: share a sem_t instead of a pthread_mutex_t issue #613: add tests for all the weird shutdown methods Add mitogen.core.now() and use it everywhere; closes #614. docs: move decorator docs into core.py and use autodecorator preamble_size: make it work on Python 3. docs: upgrade Sphinx to 2.1.2, require Python 3 to build docs. docs: fix Sphinx warnings, add LogHandler, more docstrings docs: tidy up some Changelog text
2 parents 0b9c964 + 240dc84 commit 74834c8

27 files changed

+396
-167
lines changed

ansible_mitogen/affinity.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,37 +92,37 @@
9292
_libc = ctypes.CDLL(None, use_errno=True)
9393
_strerror = _libc.strerror
9494
_strerror.restype = ctypes.c_char_p
95-
_pthread_mutex_init = _libc.pthread_mutex_init
96-
_pthread_mutex_lock = _libc.pthread_mutex_lock
97-
_pthread_mutex_unlock = _libc.pthread_mutex_unlock
95+
_sem_init = _libc.sem_init
96+
_sem_wait = _libc.sem_wait
97+
_sem_post = _libc.sem_post
9898
_sched_setaffinity = _libc.sched_setaffinity
9999
except (OSError, AttributeError):
100100
_libc = None
101101
_strerror = None
102-
_pthread_mutex_init = None
103-
_pthread_mutex_lock = None
104-
_pthread_mutex_unlock = None
102+
_sem_init = None
103+
_sem_wait = None
104+
_sem_post = None
105105
_sched_setaffinity = None
106106

107107

108-
class pthread_mutex_t(ctypes.Structure):
108+
class sem_t(ctypes.Structure):
109109
"""
110-
Wrap pthread_mutex_t to allow storing a lock in shared memory.
110+
Wrap sem_t to allow storing a lock in shared memory.
111111
"""
112112
_fields_ = [
113-
('data', ctypes.c_uint8 * 512),
113+
('data', ctypes.c_uint8 * 128),
114114
]
115115

116116
def init(self):
117-
if _pthread_mutex_init(self.data, 0):
117+
if _sem_init(self.data, 1, 1):
118118
raise Exception(_strerror(ctypes.get_errno()))
119119

120120
def acquire(self):
121-
if _pthread_mutex_lock(self.data):
121+
if _sem_wait(self.data):
122122
raise Exception(_strerror(ctypes.get_errno()))
123123

124124
def release(self):
125-
if _pthread_mutex_unlock(self.data):
125+
if _sem_post(self.data):
126126
raise Exception(_strerror(ctypes.get_errno()))
127127

128128

@@ -133,7 +133,7 @@ class State(ctypes.Structure):
133133
the context of the new child process.
134134
"""
135135
_fields_ = [
136-
('lock', pthread_mutex_t),
136+
('lock', sem_t),
137137
('counter', ctypes.c_uint8),
138138
]
139139

docs/api.rst

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
API Reference
33
*************
44

5-
.. toctree::
6-
:hidden:
7-
8-
signals
9-
105

116
Package Layout
127
==============
@@ -31,29 +26,10 @@ mitogen.core
3126
.. automodule:: mitogen.core
3227

3328
.. currentmodule:: mitogen.core
34-
.. decorator:: takes_econtext
35-
36-
Decorator that marks a function or class method to automatically receive a
37-
kwarg named `econtext`, referencing the
38-
:class:`mitogen.core.ExternalContext` active in the context in which the
39-
function is being invoked in. The decorator is only meaningful when the
40-
function is invoked via :data:`CALL_FUNCTION
41-
<mitogen.core.CALL_FUNCTION>`.
42-
43-
When the function is invoked directly, `econtext` must still be passed to
44-
it explicitly.
29+
.. autodecorator:: takes_econtext
4530

4631
.. currentmodule:: mitogen.core
47-
.. decorator:: takes_router
48-
49-
Decorator that marks a function or class method to automatically receive a
50-
kwarg named `router`, referencing the :class:`mitogen.core.Router`
51-
active in the context in which the function is being invoked in. The
52-
decorator is only meaningful when the function is invoked via
53-
:data:`CALL_FUNCTION <mitogen.core.CALL_FUNCTION>`.
54-
55-
When the function is invoked directly, `router` must still be passed to it
56-
explicitly.
32+
.. autodecorator:: takes_router
5733

5834

5935
mitogen.master
@@ -627,6 +603,14 @@ Fork Safety
627603
Utility Functions
628604
=================
629605

606+
.. currentmodule:: mitogen.core
607+
.. function:: now
608+
609+
A reference to :func:`time.time` on Python 2, or :func:`time.monotonic` on
610+
Python >3.3. We prefer :func:`time.monotonic` when available to ensure
611+
timers are not impacted by system clock changes.
612+
613+
630614
.. module:: mitogen.utils
631615

632616
A random assortment of utility functions useful on masters and children.

docs/changelog.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To avail of fixes in an unreleased version, please download a ZIP file
2222
`directly from GitHub <https://github.com/dw/mitogen/>`_.
2323

2424
Enhancements
25-
^^^^^^^^^^^^
25+
~~~~~~~~~~~~
2626

2727
* `#556 <https://github.com/dw/mitogen/issues/556>`_,
2828
`#587 <https://github.com/dw/mitogen/issues/587>`_: Ansible 2.8 is partially
@@ -61,7 +61,7 @@ Enhancements
6161

6262

6363
Mitogen for Ansible
64-
^^^^^^^^^^^^^^^^^^^
64+
~~~~~~~~~~~~~~~~~~~
6565

6666
* `#363 <https://github.com/dw/mitogen/issues/363>`_: fix an obscure race
6767
matching *Permission denied* errors from some versions of ``su`` running on
@@ -107,20 +107,21 @@ Mitogen for Ansible
107107
server has been increased from `15*3` seconds to `30*10` seconds.
108108

109109
* `#600 <https://github.com/dw/mitogen/issues/600>`_: functionality to reflect
110-
changes to ``/etc/environment`` in the running interpreter did not account
111-
for Unicode file contents. Now the file may contain data in any single byte
112-
encoding.
110+
changes to ``/etc/environment`` did not account for Unicode file contents.
111+
The file may now use any single byte encoding.
113112

114113
* `#602 <https://github.com/dw/mitogen/issues/602>`_: connection configuration
115114
is more accurately inferred for `meta: reset_connection`, the `synchronize`
116-
module, and for any other action plug-ins that establish new connections of
117-
their own.
115+
module, and for any action plug-ins that establish additional connections.
116+
117+
* `#605 <https://github.com/dw/mitogen/issues/605>`_: fix a deadlock managing a
118+
shared counter used for load balancing.
118119

119120
* `#615 <https://github.com/dw/mitogen/issues/615>`_: streaming file transfer
120-
is implemented for the ``fetch`` and any other action that transfers files
121-
from the target to the controller. Previously the file would be sent as a
122-
single message, requiring the file to fit in RAM and be smaller than internal
123-
limits on the size of a single message.
121+
is implemented for ``fetch`` and other actions that transfer files from the
122+
target to the controller. Previously the file was sent in one message,
123+
requiring it to fit in RAM and be smaller than the internal message size
124+
limit.
124125

125126
* `7ae926b3 <https://github.com/dw/mitogen/commit/7ae926b3>`_: the
126127
``lineinfile`` module began leaking writable temporary file descriptors since
@@ -188,21 +189,20 @@ Core Library
188189
:meth:`empty` method of :class:`mitogen.core.Latch`,
189190
:class:`mitogen.core.Receiver` and :class:`mitogen.select.Select` has been
190191
replaced by a more general :meth:`size` method. :meth:`empty` will be removed
191-
in Mitogen 0.3
192+
in 0.3
192193

193194
* `ecc570cb <https://github.com/dw/mitogen/commit/ecc570cb>`_: previously
194-
:meth:`mitogen.select.Select.add` would enqueue a single wake event when
195-
adding an existing receiver, latch or subselect that contained multiple
196-
buffered items, causing future :meth:`get` calls to block or fail even though
197-
data existed that could be returned.
195+
:meth:`mitogen.select.Select.add` would enqueue one wake event when adding an
196+
existing receiver, latch or subselect that contained multiple buffered items,
197+
causing :meth:`get` calls to block or fail even though data existed to return.
198198

199-
* `5924af15 <https://github.com/dw/mitogen/commit/5924af15>`_: *[security]* the
200-
unidirectional routing mode, in which contexts may only communicate with
201-
parents and never siblings (so a program cannot accidentally bridge
202-
air-gapped networks) was not inherited when a child context was initiated
203-
directly from an existing child. This did not effect the Ansible extension,
204-
since the controller initiates any new context used for routing, only forked
205-
tasks are initiated by children.
199+
* `5924af15 <https://github.com/dw/mitogen/commit/5924af15>`_: *[security]*
200+
unidirectional routing, where contexts may optionally only communicate with
201+
parents and never siblings (so that air-gapped networks cannot be
202+
unintentionally bridged) was not inherited when a child was initiated
203+
directly from an another child. This did not effect Ansible, since the
204+
controller initiates any new child used for routing, only forked tasks are
205+
initiated by children.
206206

207207

208208
Thanks!
@@ -491,7 +491,7 @@ Enhancements
491491
`#491 <https://github.com/dw/mitogen/issues/491>`_,
492492
`#493 <https://github.com/dw/mitogen/issues/493>`_: the interface employed
493493
for in-process queues changed from `kqueue
494-
<https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2>`_ / `epoll
494+
<https://www.freebsd.org/cgi/man.cgi?query=kqueue>`_ / `epoll
495495
<http://man7.org/linux/man-pages/man7/epoll.7.html>`_ to `poll()
496496
<http://man7.org/linux/man-pages/man2/poll.2.html>`_, which requires no setup
497497
or teardown, yielding a 38% latency reduction for inter-thread communication.
@@ -1037,7 +1037,7 @@ bug reports, testing, features and fixes in this release contributed by
10371037
`Josh Smift <https://github.com/jbscare>`_,
10381038
`Luca Nunzi <https://github.com/0xlc>`_,
10391039
`Orion Poplawski <https://github.com/opoplawski>`_,
1040-
`Peter V. Saveliev <https://github.com/svinota>`_,
1040+
`Peter V. Saveliev <https://github.com/svinota/>`_,
10411041
`Pierre-Henry Muller <https://github.com/pierrehenrymuller>`_,
10421042
`Pierre-Louis Bonicoli <https://github.com/jesteria>`_,
10431043
`Prateek Jain <https://github.com/prateekj201>`_,
@@ -1095,7 +1095,7 @@ Core Library
10951095

10961096
* `#300 <https://github.com/dw/mitogen/issues/300>`_: the broker could crash on
10971097
OS X during shutdown due to scheduled `kqueue
1098-
<https://www.freebsd.org/cgi/man.cgi?query=kevent>`_ filter changes for
1098+
<https://www.freebsd.org/cgi/man.cgi?query=kqueue>`_ filter changes for
10991099
descriptors that were closed before the IO loop resumes. As a temporary
11001100
workaround, kqueue's bulk change feature is not used.
11011101

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
author = u'Network Genomics'
99
copyright = u'2019, Network Genomics'
10-
exclude_patterns = ['_build']
10+
exclude_patterns = ['_build', '.venv']
1111
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinxcontrib.programoutput']
1212
html_show_copyright = False
1313
html_show_sourcelink = False

docs/internals.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ Internal API Reference
77
Internal APIs are subject to rapid change even across minor releases. This
88
page exists to help users modify and extend the library.
99

10-
.. toctree::
11-
:hidden:
12-
13-
signals
14-
1510

1611
Constants
1712
=========
@@ -50,6 +45,10 @@ Logging
5045

5146
See also :class:`mitogen.core.IoLoggerProtocol`.
5247

48+
.. currentmodule:: mitogen.core
49+
.. autoclass:: LogHandler
50+
:members:
51+
5352
.. currentmodule:: mitogen.master
5453
.. autoclass:: LogForwarder
5554
:members:
@@ -270,6 +269,8 @@ Helpers
270269
.. autofunction:: minimize_source
271270

272271

272+
.. _signals:
273+
273274
Signals
274275
=======
275276

@@ -312,10 +313,19 @@ These signals are used internally by Mitogen.
312313
- ``disconnect``
313314
- Fired on the Broker thread when disconnection is detected.
314315

316+
* - :py:class:`mitogen.core.Stream`
317+
- ``shutdown``
318+
- Fired on the Broker thread when broker shutdown begins.
319+
315320
* - :py:class:`mitogen.core.Context`
316321
- ``disconnect``
317322
- Fired on the Broker thread during shutdown (???)
318323

324+
* - :py:class:`mitogen.parent.Process`
325+
- ``exit``
326+
- Fired when :class:`mitogen.parent.Reaper` detects subprocess has fully
327+
exitted.
328+
319329
* - :py:class:`mitogen.core.Broker`
320330
- ``shutdown``
321331
- Fired after Broker.shutdown() is called.

docs/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Sphinx==1.7.1
2-
sphinxcontrib-programoutput==0.11
1+
Sphinx==2.1.2
2+
sphinxcontrib-programoutput==0.14
33
alabaster==0.7.10

0 commit comments

Comments
 (0)