Ticket #3948 defect closed fixed

Opened 4 years ago

Last modified 2 years ago

wxdemo.py does not exit

Reported by: ciphergoth Owned by:
Priority: normal Milestone:
Component: core Keywords:
Cc: itamar, khorn, jesstess, thijs, riteshn@… Branch: branches/wxreactor-stop-fix-3948
Author: exarkun Launchpad Bug:

Description

On Ubuntu "Jaunty Jackalope" i386, using the system packages for wxPython 2.8.9.1 and Twisted 8.2.0, I run

python /usr/share/doc/twisted-doc/examples/wxdemo.py

The demo window appears, but clicking on "File > Exit" or the window manager close button has no effect. Expected behaviour: the process should terminate.

I'm having trouble getting my own wxreactor-based application to exit properly, so a working demo that shows the proper way to do it would be much appreciated - thanks!

Attachments

issue_3948.patch Download (1.2 KB) - added by psykidellic 2 years ago.

Change History

1

  Changed 4 years ago by ciphergoth

  • owner glyph deleted

2

  Changed 4 years ago by jesstess

I had to add self.Destroy() before reactor.stop() in DoExit() to make the window close on clicking "File > Exit", fwiw, but the window manager close button still doesn't work and the process still doesn't terminate either way.

3

  Changed 3 years ago by michael-fig

I've confirmed this under hardy heron (Ubuntu 8.04) with Twisted-9.0.0 compiled from sources.

Even with a Destroy() added before the reactor.stop(), the process hangs (one thread is still active) and cannot be killed except with kill -9.

I am happy to help debug this, as it is a showstopper that will prevent me from using Twisted on my next project.

4

follow-up: ↓ 6   Changed 3 years ago by michael-fig

I think I found it...

The wxreactor was just calling _threadselect.stop directly, when it needed to post an event to its queue and wake it instead. This seems to solve everything (I will test more thoroughly later, but for now it gets the wxdemo working without any trouble).

Here's the replacement wxreactor.py WxReactor.stop():

    def stop(self):
        """
        Stop the reactor.
        """
        if self._stopping:
            return
        self._stopping = True
        self.callFromThread(_threadedselect.ThreadedSelectReactor.stop, self)

5

  Changed 3 years ago by khorn

  • cc khorn added

6

in reply to: ↑ 4 ; follow-up: ↓ 7   Changed 3 years ago by michael-fig

Replying to michael-fig:

I think I found it...

I would also suggest adding a note to the Twisted GUI documents: if you want to show a wx dialog from within a reactor callback, you should use wx.CallLater(myDialog.ShowModal), otherwise you'll hang the reactor until the reactor callback returns.

After discovering this tip, I'm pleased to say that the wxreactor works as expected and I'll be using it after all!

7

in reply to: ↑ 6   Changed 3 years ago by michael-fig

Here is code to monkey-patch the wxreactor if you don't have the ability to change the sources. It uses a more correct technique, in which the stop method itself is called from the main thread:

from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor

# Monkey-patch the wxreactor to make it exit correctly.
if wxreactor.WxReactor.callFromThread is not None:
    oldStop = wxreactor.WxReactor.stop
    def stopFromThread(self):
        self.callFromThread(oldStop, self)
    wxreactor.WxReactor.stop = stopFromThread

8

  Changed 3 years ago by jesstess

  • cc jesstess added

9

  Changed 3 years ago by thijs

  • cc thijs added

10

  Changed 3 years ago by itamar

  • keywords review added

We had another person with same problem, and the patch fixed things for them. Moving this to review, since there's a patch and it seems to work.

11

  Changed 3 years ago by Screwtape

I'm confused. There's no patch attached to this ticket, and no branch. Exactly what needs to be reviewed?

12

  Changed 3 years ago by itamar

Comment #4 is almost a patch.

13

  Changed 3 years ago by exarkun

  • owner set to michael-fig
  • keywords review removed

I can't reproduce the working behavior reported by making the change in comment 4. I also thought about the change for a while before testing it, and I could not think of a reason it would make a difference.

Changed 2 years ago by psykidellic

14

  Changed 2 years ago by psykidellic

  • owner michael-fig deleted
  • cc riteshn@… added
  • keywords review added

Find attached a complete patch - fix from comment 4 and top files - that fixes this issue. Using wx 2.8.10 on Ubuntu.

Basically, we need to stop the threaded wxreactor.stop from the main reactor thread rather than itself.

I really dont know how to write a test case for it but our internal GUI app (which is considerably complex as it uses bunch of custom controls) seems to work.

So if you have any suggestion on how to create one, I can do that - otherwise I really dont know how to write a testcase for this.

Also, lot of methods didnt have the 2 line breaks as mentioned in the styling guide, so I updated the same with this.

This patch applies to trunk at 30649.

15

  Changed 2 years ago by psykidellic

FWIW, the problem is there on both Linux and Mac OS X using wx 2.8.10. And the changes fixes both the environment.

16

  Changed 2 years ago by exarkun

  • branch set to branches/wxreactor-stop-fix-3948
  • branch_author set to exarkun

(In [30728]) Branching to 'wxreactor-stop-fix-3948'

17

  Changed 2 years ago by exarkun

(In [30729]) Make sure threadedselectreactor.stop() is noticed by waking up all the reactor threads when that method is called.

refs #3948

18

  Changed 2 years ago by exarkun

(In [30731]) news fragment

refs #3948

19

  Changed 2 years ago by itamar

  • keywords review removed
  • owner set to exarkun

wxdemo.py now exits correctly - merge.

20

  Changed 2 years ago by exarkun

  • status changed from new to closed
  • resolution set to fixed

(In [30740]) Merge wxreactor-stop-fix-3948

Author: exarkun Reviewer: itamar Fixes: #3948

Wake up the necessary threads in wxreactor when the reactor is stopped to ensure the reactor stops in a timely manner.

21

  Changed 2 years ago by <automation>

  • owner exarkun deleted
Note: See TracTickets for help on using tickets.