Commit Graph

444 Commits (0b99169f4208d85f592362d2466df89a2ece08b6)

Author SHA1 Message Date
David Wilson 44d36eccba issue #146: don't crash during on_broker_shutdown
There is some insane unidentifiable Mitogen context (the local context?)
that instantly crashes with a higher forks setting. It appears to be
harmless, but meanwhile this naturally shouldn't be happening.
8 years ago
David Wilson cb620500d1 issue #131: log stack and PPID with MITOGEN_ROUTER_DEBUG=1 8 years ago
David Wilson d58b5ad777 core: prevent creation of unicode Message.data
Was triggering a crash indirectly due to Ansible passing us Unicode
strings. Needs a better fix.
8 years ago
Alex Willmer f999b9adbf Crank zlib.compress() upto 9
SSH command size: 482 bytes (no change)
Preamble size: 8946 bytes (down 33)
8 years ago
David Wilson b243da087c issue #121: fix call_function_test by not raising the dead
A first small mea culpa to all my testing sins of late :)
8 years ago
David Wilson f1009b7502 issue #121: fix breakage caused by a9c6c13
This actually addresses multiple problems:

* Single-file programs were broken, since the fix introduced in
  6931cc10c4 caused builtin_find_module()
  to start indicating __main__ can always be loaded locally. That's
  broken, and there might be more cases where the same problem will crop
  up.

  Since it was indicated __main__ could be loaded locally, the built-in
  import machinery was allowed to attempt that (since we remove __main__
  from sys.modules during bootstrap), which caused a safety check to
  fire in the bowels of Python:

      "Cannot re-init internal module %.200s"

* The check for presence of the whitelist was totally broken, since the
  whitelist is never an empty list. Therefore 'self' was being returned
  for every module, including extension modules like 'termios'.

I have hand-verified this does not break the fix for issue #113. I
looked at writing a test for that, but it requires a Docker container
(or similar) with an ancient version of Ansible installed. Will open a
separate ticket tracking this.
8 years ago
David Wilson 5dddee62ea Revert "issue #121: minimal fix for nested_test."
Mega broken.

This reverts commit a7dbbd96aa.
8 years ago
David Wilson a0c4df72b0 issue #121: minimal fix for nested_test. 8 years ago
David Wilson 28afa955a3 importer: take priority over system packages when whitelisting is enabled
Might want to de-overload the meaning of whitelist in future, but in
the meantime it works fine for Ansible and I can't think of a
whitelisting use case that would break because of it.

Closes #114.
8 years ago
David Wilson cf01c6b710 importer: avoid duplicate module load(!); closes #113.
Amazed this one managed to scrape through for so long. Calling
__import__ from within find_module() was causing the target module, in
this case cookielib, to be loaded *then overwritten* by a subsequent
duplicate load higher in the stack.

The result is that cookielib was loaded twice, and, per usual Python
import semantics, a reference to the partially initialized first
cookielib was installed in sys.modules while its code executed.

At the end of cookielib on 2.x, it imports _LWPCookieJar, which in turn
imports the partially built cookielib from sys.modules, then subclasses
the CookieJar from /that/ module.

Everything is wonderful. Then the call returns back up into the import
mechanism which restarts the entire process -- only this time,
_LWPCookieJar is /not/ reinitialized, so the copy in sys.modules is
still left with types pointing at the old module!

So the duplicate import creates a new CookieJar which is not the base
class of LWPCookieJar. Tada! 3 hours debugging.

This is probably a performance fix in disguise, didn't realize things
were so broken. It may also be a regression elsewhere. Urgently need to
finish the tests.
8 years ago
David Wilson ff617824a1 ansible: fix some flake8 errors
* Unused imports
* Undefined names in helpers.py
* Copyright header wrapping
8 years ago
Alex Willmer 33781aba2c core: Correct naming of Stream.sent_modules
Fixes #90
8 years ago
Alex Willmer a1aab30e63 core: Implement Dead.__ne__ & Dead.__hash__
Both these addtions are to address warnings in
https://lgtm.com/projects/g/dw/mitogen/alerts/?mode=list. Namely that if
a class defines an equality method then it should also define an
inequality and a hash method.

Refs #61
8 years ago
Alex Willmer 4b373c421b core: Standardise type of Importer.whitelist
This seemed a reasonable streamlining, but I'm happy to be overruled.
8 years ago
Alex Willmer ecaa8609f3 core: Add docstring to is_blacklisted_import()
This documents the existing behaviour, which may not be the intended.
8 years ago
David Wilson 5855f1739f core: Handle unpicklable data in dispatch_calls()
Sending just via .call_async() would previously crash the child, now it
generates CallError like intended.
8 years ago
David Wilson d4169557f1 Fix some more Python 2.4 syntax 8 years ago
David Wilson afc8697288 core: Ensure add_handler() callbacks really receive _DEAD on shutdown 8 years ago
David Wilson 020036f807 core: add a nasty hack for Ansible modules. 8 years ago
David Wilson 4d940f08ae importer: drop redundant prefix from pkg_present
For the 52 submodules of ansible.modules.system, this produced a 1602
byte pkg_present list. After stripping it becomes 406 bytes, and the
entire LOAD_MODULE size drops from 1988 bytes to 792 bytes (-60%).

For the 68 submodules of ansible.module_utils, 1902 bytes pkg_present
becomes 474 bytes (-75%), and LOAD_MODULE size drops from 2867 bytes to
1439 bytes (-49%).

In a simple test running Ansible's "setup" module followed by its "apt"
module, wire bytes sent drops from 140,357 to 135,531 (-3.4%).
8 years ago
David Wilson b543b84e80 importer: share blacklist logic between master/parent 8 years ago
David Wilson 8ec6ae1da0 importer: module whitelist/blacklist support
Hoped to avoid it, but it's the obvious solution for Ansible.
8 years ago
David Wilson 43ba1c76dc core: wrap selects in EINTR handlers
This isn't nearly enough, but it catches the most common victim of
EINTR.
8 years ago
David Wilson aafe458a13 core: #39: don't call logging framework when logging is disabled
It looks ugly as sin, but this nets about a 20% drop in user CPU time,
and close to 15% increase in throughput.

The average log call is around 10 opcodes, prefixing with '_v and' costs
an extra 2, but both are simple operations, and the remaining 10 are
skipped entirely when _v or _vv are False.
8 years ago
Alex Willmer 3261c561dd Fix AttributeError in mitogen.core.Context.send_await()
As of adc8fe3aed Receiver objects do not
have a get_data() method and Receiver.get() does not unpickle the
message.
8 years ago
David Wilson 6905dc4e8d master: use queue-like Latch in Select() too. 8 years ago
David Wilson 20afa5b90c Latch v2: combined queue + one self-pipe-per-thread
Turns out it is far too easy to burn through available file descriptors,
so try something else: self-pipes are per thread, and only temporarily
associated with a Lack that wishes to sleep.

Reduce pointless locking by giving Latch its own queue, and removing
Queue.Queue() use in some places.

Temporarily undo merging of of Waker and Latch, let's do this one step
at a time.
8 years ago
David Wilson e6a107c5aa core: replace Queue with Latch
On Python 2.x, operations on pthread objects with a timeout set actually
cause internal polling. When polling fails to yield a positive result,
it quickly backs off to a 50ms loop, which results in a huge amount of
latency throughout.

Instead, give up using Queue.Queue.get(timeout=...) and replace it with
the UNIX self-pipe trick. Knocks another 45% off my.yml in the Ansible
examples directory against a local VM.

This has the potential to burn a *lot* of file descriptors, but hell,
it's not the 1940s any more, RAM is all but infinite. I can live with
that.

This gets things down to around 75ms per playbook step, still hunting
for additional sources of latency.
8 years ago
David Wilson a35fcf44cc ansible: restructure to avoid intermediate imports 8 years ago
David Wilson f3e51a7b18 core: CALL_FUNCTION should check auth_id, not src_id 8 years ago
David Wilson 32f6ee7d43 issue #40: mitogen.unix initial implementation. 8 years ago
David Wilson e63e9d299e docs: add Message documentation 8 years ago
David Wilson 10230f62dd core: Message.reply() helper function 8 years ago
David Wilson 6fc8fa5b22 core: Don't crash if a stream is missing a side. 8 years ago
David Wilson 9238e09ae8 core: Restore behaviour of unpickling Router-specific Context subclass 8 years ago
David Wilson dd088908df select: clean up API. 8 years ago
David Wilson df07e47d24 core: de-munge Message.unpickle() vs. Receiver.get(). 8 years ago
David Wilson a39cd44bf2 core: add auth_id field. 8 years ago
David Wilson a54c96faae core: remove unused SecurityError. 8 years ago
David Wilson 07d4d799f1 Add mitogen.main() decorator mainly for docs and demo use. 8 years ago
David Wilson 55c23e1c57 issue #68: replace sets with lists
Fix a MyPy warning by only passing lists to select.select(). At least on
Python 2.x, select.select() was internally converting the sets to lists
anyway.

By the time lists become inefficient here, it is likely that
select.select() itself will also be inefficient, and need replaced with
.poll() or similar.

No discernible performance different when transferring django.db.models
to a local VM.
8 years ago
David Wilson a0d9d34231 core: fix profiling
* SIGTERM safety net prevents profiler from writing results, so disable
  it when profiling is active.
* fix warning corrupting stream when profiling=True
8 years ago
David Wilson 5f2fa2cda6 importer: always refuse builtins and __builtin__. 8 years ago
David Wilson 0f899f34ff importer: new format to signal ImportError
Previously we'd send just None in GET_MODULE reply, but now since there
is no single request-reply structure, we must include the fullname in
the LOAD_MODULE response and make all of its data fields None to
indicate the same.
8 years ago
David Wilson 4d01dc3ba6 Initial pass at module preloading
* Don't implement the rules for when preloading occurs yet
* Don't attempt to streamily preload modules downstream while this
  context hasn't yet received the final module. There is quite
  significant latency buried in here, but for now it's a lot of work to
  fix.

This works well enough to handle at least the mitogen package, but it's
likely broken for anything bigger.
8 years ago
David Wilson ed71ae72f8 master: make mitogen minimally functional under gevent
It seems gevent automatically sets blocking behaviour on fds produced by
the socket module, which causes the Python process we fork to fail
horribly. So in the child, always reset the blocking flag.
8 years ago
David Wilson 326886832e Add license text everywhere. 8 years ago
Alex Willmer 3831ac360f Replace all calls to file() with open()
Although these are synonyms in Python 2.x, when using MyPy to typecheck
code use of file() causes spurious errors.

This commit also serves as one small step to Python 3.x compatibility,
since 3.x removes the file() builtin.
8 years ago
David Wilson 0481c08beb Ensure _run_defer() fully executes at least once before shutdown
Without this, it's possible for Waker to be start_received() after the
shutdown signal has already been sent, resulting in 5 second delay
during shutdown.

Additionally mask EBADF during os.write() to waker's write side.
Necessary since nothing synchronizes writer threads from the broker
thread during shutdown. Could be done with a lock instead, but this is
cheaper.
8 years ago
David Wilson b1ad04330b docs: move Router.route() into Sphinx. 8 years ago
David Wilson fb759f7c16 docs: move Broker docstrings into Sphinx. 8 years ago
David Wilson ffa063cc01 docs: annother barriage of cross-reference fixes. 8 years ago
David Wilson 7f3a58d514 core: Remove unused on_shutdown attribute. 8 years ago
David Wilson ec66152e37 docs: better io_op doc, move Side docs to Sphinx. 8 years ago
David Wilson 0767abf26f docs: move BasicStream docs into Sphinx. 8 years ago
David Wilson b7a9aa46cf core: More robust shutdown
Now there is a separate SHUTDOWN message that relies only on being
received by the broker thread, the main thread can be hung horribly and
the process will still eventually receive a SIGTERM.
8 years ago
David Wilson 3285fc2f75 Implement test_aborted_on_local_context_disconnect 8 years ago
David Wilson 690ee6dbe2 Fix select_test failure, remove crap old timing_test. 8 years ago
David Wilson 2454dcc990 core: loosen assertion to allow fakessh_test to succeed. 8 years ago
David Wilson 9b13a4cc61 Fix 2 call_function_test failures. 8 years ago
David Wilson f1d82c7284 More API documentation. 8 years ago
David Wilson b7f95e558f Better document Router API and constructors. 8 years ago
David Wilson 815f23bddd Sense of block= parameter was inverted. 8 years ago
David Wilson c4d9f124c6 Document Sender and Receiver classes. 8 years ago
David Wilson 849ccebe04 receiver: only permit one notify callback
There is no point spamming a list for every function call, there is no
use case where multiple notify callbacks would be useful.
8 years ago
David Wilson 48bf987570 issue #20: fix queue.get() parameter list. 8 years ago
David Wilson e3d967ebeb issue #20: initial implementation of mitogen.master.Select(). 8 years ago
David Wilson 14783c75e8 issue #9: log warning when a cross-sibling CALL_FUNCTION occurs
First step to making it a fatal error.
8 years ago
David Wilson 9de1fca3bf issue #9: ensure messages arrive on the expected stream
If no ADD_ROUTE message has been received from the master associating a
stream with a particular context ID, then it is expected messages
originating from that context ID can only be routed via the parent.
8 years ago
David Wilson 01729b18a5 core: use an output deque rather than string to improve worst case perf
This probably worsens performance in the common case, but it prevents
runaway producers (see e.g. issue #36) from spending all their CPU
copying around huge strings.

It's also a small step towards a solution to issue #6, which will
replace the output buffer with some sort of fancier queue anyway.

This reduces a particular 40 second run of rsync to 1.5 seconds.
8 years ago
David Wilson effe4117e1 Treat EPIPE as disconnect too; needed for fakessh. 8 years ago
David Wilson 9c4bf37cfc Remove final vestiges of context.key. 8 years ago
David Wilson 05cc74d142 core: Support profiling 8 years ago
David Wilson a9387b0504 core: remove pointless eval() of ARGV0 environment variable. 8 years ago
David Wilson fb9ce1054c core: set O_NONBLOCK on every side. Closes #33
The last time I tested set_nonblock() as a fix for the rsync hang, I
used F_SETFD rather than F_SETFL, which resulted in no error, but also
did not set O_NONBLOCK. Turns out missing O_NONBLOCK was the problem.

The rsync hang was due to every context blocking in os.write() waiting
for either a parent or child buffer to empty, which was exacerbated by
rsync's own pipelining, that allows writes from both sides to proceed
even while reads aren't progressing. The hang was due to os.write() on a
blocking fd blocking until buffer space is available to complete the
write. Partial writes are only supported when O_NONBLOCK is enabled.
8 years ago
David Wilson 7a60b20dc6 core: Generalize/duplicate the call/send_await code using Receiver. 8 years ago
David Wilson e4c832685d core: synchronize Stream._output_buf by deferring send()
Previously _output_buf was racy. This may or may not be cheaper than
simply using a lock, but it requires much less code, so I prefer it for
now.
8 years ago
David Wilson ead67de883 core: make Side.write() return None rather than crash if side already closed. 8 years ago
David Wilson 74b31bbe47 core: better Message.__repr__. 8 years ago
David Wilson 2a365aa9b0 Replace `with_context` parameter with mitogen.core.takes_econtext decorator 8 years ago
David Wilson 4244a4609c Reduce CHUNK_SIZE to paper over a hang with rsync 8 years ago
David Wilson c67119501b Keep allocate_id() in the enhanced router class. 8 years ago
David Wilson 4720eb1c55 core: add ALLOCATE_ID message for fakessh. 8 years ago
David Wilson e796487cca core: allow sending 0-byte messages. 8 years ago
David Wilson b809d43865 Move more docstrings out of core.py. 8 years ago
David Wilson 918edf5145 Add TODO 8 years ago
David Wilson 502266f115 Fix Channel constructor and add simple test; closes #32 8 years ago
David Wilson 4f50707b82 core: support takes_econtext and takes_router decorators. 8 years ago
David Wilson 38a9482860 Add hacks to allow Mock to be imported. 8 years ago
David Wilson 1f99dcb435 fix unbelievably dumb variable shadowing 8 years ago
David Wilson be9e55fe8c pickle: support Context(), use same unpickler everywhere.
* Support passing Context() objects in function calls and return values.
  Now the fakessh demo from the documentation index would work
  correctly.

* Since slaves can communicate with each other now, they should also use
  the same approach to unpickling as the master already used. Collapse
  away all the unpickle extension crap and hard-wire just the 3 types
  that support unpickling.
8 years ago
David Wilson 11acc031a9 pickle: Prevent access to the _Dead and CallError constructors
This should be pretty much identical the same behaviour as before, but
the extra assertion makes me feel happier.
8 years ago
David Wilson e75f1d8579 Add call_function_test, fix various exception bugs. 8 years ago
David Wilson e7ff6259a3 Initial commit. 8 years ago