Commit Graph

180 Commits (2ecc6f43a4492101d85c07935bc735932849d149)

Author SHA1 Message Date
David Wilson 07d4d799f1 Add mitogen.main() decorator mainly for docs and demo use. 7 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.
7 years ago
David Wilson 09eb3fd9ba utils: support log_to_file(usec=True) 7 years ago
David Wilson 2848d35aff importer: warn on duplicate request, simplify preload logic
* Children should never generate a request for a module that has already
  been sent, however there are a variety of edge cases where, e.g.
  asynchronous calls are made into unloaded modules in a set of
  children, causing those children to request modules (and deps) in a
  different order, which might break deduplication. So add a warning to
  catch when this happens, so we can figure out how to handle it.
  Meanwhile it's only a warning since in the worst case, this just adds
  needless latency.

* Don't bother treating sent packages separately, there doesn't seem to
  be any need for this (after docs are updated to match how preloading
  actually works now).
7 years ago
David Wilson 2e729e54cc importer: fix glaring bug in find_related()
Overwriting 'fullname' variable caused basically nonsensical filtering.
Result was including the module being searched in the list of
dependencies, which was causing ModuleResponder to send it early, which
was causing contexts to start importing the module before preloading of
dependencies had completed.
7 years ago
David Wilson 0dbb1ec028 importer: warn once about missing source and cache negative hit 7 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
7 years ago
David Wilson b158259c86 Split up parent and master modules
Knocks 4kb off network footprint for a proxy connection.
7 years ago
David Wilson 5f2fa2cda6 importer: always refuse builtins and __builtin__. 7 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.
7 years ago
David Wilson 07ae14f11d importer: semi-functional preloader
Doesn't yet implement the rules in the docs, but I think the doc rules
could maybe change to match this. Needs lots of cleanup work and
thorough testing, but this is a great start.
7 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.
7 years ago
David Wilson b75e77b410 master: force set_block() in tty_create_child too.
For gevent, just as in 5f7633cd56
7 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.
7 years ago
David Wilson 247f50e08d master: add comment 7 years ago
David Wilson bbcfc585a8 master: add a comment to explain what's going on, and fix log msg.
Closes #70
7 years ago
David Wilson 3cbe9baf6d issue #70: remove redundant 'raise SystemExit'
Either execvp succeeds, in which case the process image is replaced, or
execvp throws an exception, in which case control flow exits the frame
anyway.
7 years ago
David Wilson 326886832e Add license text everywhere. 7 years ago
David Wilson f1f36cec35 Simplify the API, make Broker optional and auto-shutdown on main thread exit. 7 years ago
David Wilson bbcf1a0bd4 Fix confusing return statements, closes #67. 7 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.
7 years ago
David Wilson c09dcd82a7 Fix Python 3 fix :/ closes #57 7 years ago
Alex Willmer 77d57a7e38 fakessh: Remove colon causing SyntaxError
fixes #58
7 years ago
David Wilson 658806bde8 fakessh: interruptible wait compatible with <2.7; closes #55 7 years ago
David Wilson b3491b190c master: Lazy-format more logging arguments, minor 3.x compat increments 7 years ago
Alex Willmer 0e7cc55e33 first stage: Remove/dedent else block
Since the above if block ends in a call to os.execv() this block will
only ever run when the if condition was false. Hence putting it in an
else clause is unnecessary.
7 years ago
Alex Willmer d1c10f64a5 first stage: Use string concatenation 7 years ago
Alex Willmer b490cde604 first stage: eliminate whitespace 7 years ago
Alex Willmer c7c0266e99 first stage: single letter variables 7 years ago
David Wilson 3d0e8c36d2 issue #49: 2.x/3.x compatible decode.
Much uglier command line, but it works across major Python releases.
7 years ago
David Wilson db9bec8720 Implement Alex's awesome idea of zlibbing the first stage too! Closes #49 7 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.
7 years ago
David Wilson 828f60351b importer: Delete _get_module_via_parent entirely
Can't figure out what it's supposed to do any more, and can't find a
version of Ansible before August 2016 (when I wrote that code) that
seems to need it.

Add some more mitigations to avoid sending dylibs.
7 years ago
David Wilson b1ad04330b docs: move Router.route() into Sphinx. 7 years ago
David Wilson fb759f7c16 docs: move Broker docstrings into Sphinx. 7 years ago
David Wilson ffa063cc01 docs: annother barriage of cross-reference fixes. 7 years ago
David Wilson 7f3a58d514 core: Remove unused on_shutdown attribute. 7 years ago
David Wilson c9daa2ff30 docs: move fakessh docs into Sphinx. 7 years ago
David Wilson 6a10ab605e docs: move mitogen.utils docs to Sphinx. 7 years ago
David Wilson ec66152e37 docs: better io_op doc, move Side docs to Sphinx. 7 years ago
David Wilson 0767abf26f docs: move BasicStream docs into Sphinx. 7 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.
7 years ago
David Wilson 3285fc2f75 Implement test_aborted_on_local_context_disconnect 7 years ago
David Wilson 690ee6dbe2 Fix select_test failure, remove crap old timing_test. 7 years ago
David Wilson 15bf0f54e2 Beginnings of module_finder_test 7 years ago
David Wilson 2454dcc990 core: loosen assertion to allow fakessh_test to succeed. 7 years ago
David Wilson 9b13a4cc61 Fix 2 call_function_test failures. 7 years ago
David Wilson 79dd00db5a master: hack to avoid executing __main__. 7 years ago
David Wilson 6f5d4882da We don't support sets, so don't use them in module loader(!) 7 years ago
David Wilson ecd39e4f2a add missing IOLOG, fixes _iter_read/SshTest. 7 years ago
David Wilson 451fb347d0 master: include related modules in ModuleResponder response 7 years ago
David Wilson 4af920094a iter_read: also treat 0-byte read as disconnection 7 years ago
David Wilson d169b55d64 select: fix typo 7 years ago
David Wilson af6812b458 Fix context naming for sudo and via=. 7 years ago
David Wilson f1d82c7284 More API documentation. 7 years ago
David Wilson b7f95e558f Better document Router API and constructors. 7 years ago
David Wilson 815f23bddd Sense of block= parameter was inverted. 7 years ago
David Wilson c4d9f124c6 Document Sender and Receiver classes. 7 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.
7 years ago
David Wilson 1be10575a1 importer: Don't respond to non-.py[co] imports. 7 years ago
David Wilson 48bf987570 issue #20: fix queue.get() parameter list. 7 years ago
David Wilson f869e088f8 issue #20: tests and fixes for mitogen.master.Select(). 7 years ago
David Wilson e3d967ebeb issue #20: initial implementation of mitogen.master.Select(). 7 years ago
David Wilson 14783c75e8 issue #9: log warning when a cross-sibling CALL_FUNCTION occurs
First step to making it a fatal error.
7 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.
7 years ago
David Wilson 4de321a3b0 Adjust get_boot_command to match _first_stage change
inspect module includes the staticmethod decorator added by
a8d1dc6730
7 years ago
David Wilson c810f66bcc issue #19: whoops, implement sliding window properly 7 years ago
David Wilson 43ccbf0459 issue #19: second attempt at import scanner
This version is based on the modulefinder standard library module,
pruned back just to handle modules we know have been loaded already, and
to scan module-level imports only, rather than imports occurring in
class and function scope (crappy heuristic, but assume they are lazy
imports).

The ast and compiler modules were far too slow, whereas this version can
bytecode compile and scan all the imports for django.db.models (58
modules) in around 200ms.. 3.4ms per dependency, it's probably not going
to get much faster than that.
7 years ago
David Wilson 175abee2c9 Fix inverted logic. 7 years ago
David Wilson 8054fa867a Only import compiler when necessary, it's not available in 3.x. 7 years ago
Alex Willmer aa817f1b83 Handle failure to import the ast module 7 years ago
Alex Willmer dd8271f061 Fix stale references to m.master.connect & m.ssh.connect
I'm fairly sure these updates are correct, but I'm not able to run the
test suite to confirm.
7 years ago
Alex Willmer d706b7d6b8 Update lingering references to with_broker & run_with_broker
A previous commit renamed run_with_broker() and with_broker() to
run_with_router() and with_router() respctively. Some references were
missed.
7 years ago
Alex Willmer dc26460a28 Add missing LOG import
Fixes an undefined variable error found by pylint
7 years ago
Alex Willmer 3b24314a8b Use fully qualified Context
Fixes an undefined variable error found by pylint
7 years ago
Alex Willmer 57a0aa26bf Add missing import of AnsibleError
Found with pylint
7 years ago
Alex Willmer f2ea38a963 Add missing auth_incorrect_msg used to in exception message
Found using pylint
7 years ago
Alex Willmer 1f955fc655 Declare self-less method as static
Found using pylint
7 years ago
Alex Willmer 36754254b2 Add missing imports: ast, compiler
Found using pylint
7 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.
7 years ago
David Wilson eb060e1a0e Document Router.profiling. 7 years ago
David Wilson effe4117e1 Treat EPIPE as disconnect too; needed for fakessh. 7 years ago
David Wilson 9c4bf37cfc Remove final vestiges of context.key. 7 years ago
David Wilson 05cc74d142 core: Support profiling 7 years ago
David Wilson b827ee1bc7 Delete mitogen.utils.log_to_tmp() 7 years ago
David Wilson 400c971f4a fakessh: SSH server uses user's shell to parse command line. 7 years ago
David Wilson 04ea5306e9 Document a bunch of mitogen.master and move more docstrings into Sphinx. 7 years ago
David Wilson b2f13f1fa4 master: propagate routes for IDs allocated via ALLOCATE_ID
needed for inter-child rsync.
7 years ago
David Wilson 750e9fab24 Make log_to_file accept string level rather than logging package constant
Saves an import, pain in the ass to type all the time.
7 years ago
David Wilson a9387b0504 core: remove pointless eval() of ARGV0 environment variable. 7 years ago
David Wilson 1411790f56 master: make write_all() handle O_NONBLOCK and deadlines. 7 years ago
David Wilson 2ee7309378 master: send_await() always unpickles now. 7 years ago
David Wilson 769ece2514 fakessh: improve logging slightly.
Makes it easier to match up reads/writes across log files by using
message byte count
7 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.
7 years ago
David Wilson db225638f0 core: Make iter_read() handle deadline (and non-blocking IO) properly 7 years ago
David Wilson 7a60b20dc6 core: Generalize/duplicate the call/send_await code using Receiver. 7 years ago
David Wilson 76d35df889 master: use decorators rather than call_with_deadline(with_context=..) 7 years ago
David Wilson 76d1e66790 fakessh: use decorators rather than call_with_deadline(with_context=..) 7 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.
7 years ago
David Wilson ead67de883 core: make Side.write() return None rather than crash if side already closed. 7 years ago