Opened 6 years ago

Closed 2 years ago

Last modified 2 years ago

#3270 defect closed fixed (fixed)

The XMPP example in Twisted.Words crashes if I stop it with Ctrl-C

Reported by: jozilla Owned by: exarkun
Priority: normal Milestone:
Component: words Keywords:
Cc: Branch: branches/xmpp_client.py-3270-3
(diff, github, buildbot, log)
Author: exarkun Launchpad Bug:

Description (last modified by exarkun)

The XMPP example (xmpp_client.py) in Twisted words throws an exception when I quit it with Ctrl-C:

Disconnected.
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 228, in mainLoop
    self.runUntilCurrent()
  File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 561, in runUntilCurrent
    call.func(*call.args, **call.kw)
  File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 414, in _continueSystemEvent
    callable(*args, **kw)
  File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 375, in disconnectAll
    failure.Failure(main.CONNECTION_LOST))
--- <exception caught here> ---
  File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 48, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.5/site-packages/twisted/python/log.py", line 33, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.5/site-packages/twisted/python/context.py", line 37, in callWithContext
    return func(*args,**kw)
  File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 576, in connectionLost
    Connection.connectionLost(self, reason)
  File "/usr/lib/python2.5/site-packages/twisted/internet/tcp.py", line 416, in connectionLost
    protocol.connectionLost(reason)
  File "/usr/lib/python2.5/site-packages/twisted/words/xish/xmlstream.py", line 82, in connectionLost
    self.dispatch(self, STREAM_END_EVENT)
  File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 227, in dispatch
    self._eventObservers[(priority, event)].callback(object)
  File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 37, in callback
    methodwrapper(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/twisted/words/xish/utility.py", line 21, in __call__
    self.method(*nargs, **nkwargs)
  File "xmpp_client.py", line 58, in disconnected
    reactor.stop()
  File "/usr/lib/python2.5/site-packages/twisted/internet/base.py", line 342, in stop
    raise RuntimeError, "can't stop reactor that isn't running"
exceptions.RuntimeError: can't stop reactor that isn't running

I already posted this on the mailing list. Here is the comment by Ralph Meijer:

=================
I haven't seen that behavior before, but I can confirm this happens. I
poked around a bit with the example. It seems that the socket is closed
before its factory is shut down. It might be that the reactor shutdown
procedure has changed, so I think this behavior is the result of a bug,
either in the example or somewhere else.
=================

Change History (16)

comment:1 Changed 6 years ago by exarkun

  • Description modified (diff)

reformatting description

comment:2 Changed 6 years ago by exarkun

  • author set to exarkun
  • Branch set to branches/xmpp_client.py-3270

(In [23876]) Branching to 'xmpp_client.py-3270'

comment:3 Changed 6 years ago by exarkun

I'm not going to finish this now. radix convinced me that we should not introduce a new public API which is only capable of being used as twisted.internet.task.react in this branch is capable of being used. Instead, #1490 should do something about this use-case and the examples should be updated to use that solution.

comment:4 Changed 6 years ago by ralphm

exarkun: could you expand a bit on why the current behaves as it does? I don't remember seeing such exceptions when the example code was written. I assume the idiom used in the example is not correct? Also, do you have some kind of work around that could be used until what you suggest should happen on #1490 has been completed?

comment:5 Changed 6 years ago by exarkun

It calls reactor.stop() in its connection-lost handler. Also, C calls reactor.stop() and disconnects everything. It needs to keep track of whether the reactor is already in the process of shutting down or not so that it can skip the call if it is. This is basically what I implemented in a general way in the branch.

comment:6 Changed 6 years ago by exarkun

And I love trac markup.

comment:7 Changed 4 years ago by <automation>

  • Owner exarkun deleted

comment:8 Changed 2 years ago by exarkun

  • Branch changed from branches/xmpp_client.py-3270 to branches/xmpp_client.py-3270-2

(In [34897]) Branching to 'xmpp_client.py-3270-2'

comment:9 Changed 2 years ago by exarkun

  • Branch changed from branches/xmpp_client.py-3270-2 to branches/xmpp_client.py-3270-3

(In [35697]) Branching to 'xmpp_client.py-3270-3'

comment:10 Changed 2 years ago by exarkun

  • Keywords review added
  • Owner set to glyph

Years later, #1490 is still open, so guess we'll go with this.

Build results

comment:11 Changed 2 years ago by therve

  • Keywords review removed
  • Owner changed from glyph to exarkun

OK, I buy that.

  • One (blocking) pyflakes: doc/words/examples/xmpp_client.py:23: undefined name 'client_jid'
  • It most likely deserves some documentation. Please open a bug if you're not tackling it there.

Thanks, please merge!

comment:12 Changed 2 years ago by exarkun

(In [35702]) Slightly improve the react docstring

refs #3270

comment:13 Changed 2 years ago by exarkun

  • Resolution set to fixed
  • Status changed from new to closed

(In [35703]) Merge xmpp_client.py-3270-2

Author: exarkun
Reviewer: therve
Fixes: #3270

Add twisted.internet.task.react, a helper for running and stopping the reactor.

comment:14 Changed 2 years ago by glyph

Would it be nice to also have a decorator-syntax version of this?

comment:15 follow-up: Changed 2 years ago by exarkun

I don't know, would it? I feel like no. The decorator form (I am guessing, since "decorator-syntax version" is a long way away from a complete specification - if I guessed wrong it's your fault :) would take a perfectly reasonable Deferred-returning function and make it so you can only call it once, and only if the reactor is not running yet.

comment:16 in reply to: ↑ 15 Changed 2 years ago by glyph

Replying to exarkun:

I don't know, would it? I feel like no. The decorator form (I am guessing, since "decorator-syntax version" is a long way away from a complete specification - if I guessed wrong it's your fault :) would take a perfectly reasonable Deferred-returning function and make it so you can only call it once, and only if the reactor is not running yet.

Yeah, that would be the naive implementation. I did implement this once: amphacks.util.mainpoint. It checks the function's __module__ and only runs it if it's in __main__, in that implementation.

Note: See TracTickets for help on using tickets.