[Twisted-Python] buildProtocol never called
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
Hello, First let me introduce a piece of software I have written using Twisted: it's called dfd_keeper[1], and is a BSD/pf port of a generic concept I invented called the dynamic firewall daemon[2]. [1] http://www.subspacefield.org/security/dfd_keeper/ [2] http://www.subspacefield.org/security/dfd/ If you like the concept, please promote the idea; I think it's neat but there hasn't exactly been a groundswell of interest in it. In essence, it allows you to netcat or telnet to the daemon process and issue text-like commands that alter your firewall rules. You can think of it as a "shell" for the firewall, and it can do neat things like create firewall rules that expire automatically after a period of time and so on. The problem I'm having is this; I upgraded my firewall from OpenBSD 4.2 or so to OpenBSD 4.5, and now I'm working with newer versions of all the software including Twisted. The program works fine normally, and can run in the background, but if I invoke a daemonize() routine that turns it into a network daemon, it refuses to serve incoming TCP connections. Actually the TCP connection is made, but the software never responds to it. Specifically, my twisted.internet.protocol.Factory instance is created, but buildProtocol is never called. Before I dive deeply into debugging the software, I thought I'd ask if anyone knew off the top of their head any reason why Twisted might not act the same if it was daemonized (no controlling terminal, no stdin/out/err, forked as a background process). Thanks! Travis H. -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 06:17:54PM -0500, travis+ml-twisted@subspacefield.org wrote:
Here's some behavior I've gathered through debugging. Note that all of the daemonization occurs before starting the reactor. If I don't close any file descriptors during daemonization, the program works fine. If I close all file descriptors (3, 4, 5, 6, 7) and above, it works, but upon disconnecting, I get the following error message: File "/etc/keeper_screen", line 675, in <module> reactor.run() File "/usr/local/lib/python2.5/site-packages/twisted/internet/base.py", line 1128, in run self.mainLoop() --- <exception caught here> --- File "/usr/local/lib/python2.5/site-packages/twisted/internet/base.py", line 1140, in mainLoop self.doIteration(t) File "/usr/local/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 126, in doSelect self._preenDescriptors() File "/usr/local/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 88, in _preenDescriptors self._disconnectSelectable(selectable, e, False) File "/usr/local/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 196, in _disconnectSelectable selectable.connectionLost(failure.Failure(why)) File "/usr/local/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 150, in connectionLost os.close(fd) exceptions.OSError: [Errno 9] Bad file descriptor If I close all file descriptors two and above, I get that error message immediately. If I close all file descriptors eight and above, the program works normally. Is there some code in Twisted that expects certain file descriptors to be open? Anyone got a clue what could be happening here? -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sat, 30 May 2009 18:36:16 -0500, travis+ml-twisted@subspacefield.org wrote:
Creating the reactor at all may create a file descriptor which is necessary for its operation. Closing these will very likely cause problems. Also, Twisted has daemonization features already which are known to work well. ;) Why aren't you using these? Jean-Paul
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 08:26:40PM -0400, Jean-Paul Calderone wrote:
Creating the reactor at all may create a file descriptor which is necessary for its operation. Closing these will very likely cause problems.
This is all before calling any Twisted routines
Also, Twisted has daemonization features already which are known to work well. ;) Why aren't you using these?
Hadn't heard about it. Where's the documentation? -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/3f8b0ee4e56cebdb9430ed8d08e7b7c0.jpg?s=120&d=mm&r=g)
travis+ml-twisted@subspacefield.org wrote:
look for "twistd": http://twistedmatrix.com/projects/core/documentation/man/twistd-man.html http://twistedmatrix.com/projects/core/documentation/howto/basics.html http://twistedmatrix.com/projects/core/documentation/howto/tap.html http://twistedmatrix.com/projects/core/documentation/howto/application.html Johann
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sun, 31 May 2009 15:18:50 -0500, travis+ml-twisted@subspacefield.org wrote:
It's pretty easily to unintentionally import and instantiate the default reactor. Are you certain daemonization all happens before that?
It's a feature of twistd. http://twistedmatrix.com/projects/core/documentation/howto/tap.html and http://twistedmatrix.com/projects/core/documentation/howto/application.html talk about how to structure your code so as to be able to take advantage of the features of twistd. Jean-Paul
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 06:17:54PM -0500, travis+ml-twisted@subspacefield.org wrote:
I ran ktrace/kdump against the binary and it appears that python likes to open file handles to the script and certain modules for file descriptors 5-7 or so (depending on how much you import), and if you close those file descriptors, unusual things happen when python tries to access them. In the end I decided it was not important enough to close all the parent's file descriptors, and it runs fine as a deamon leaving them open. -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 06:17:54PM -0500, travis+ml-twisted@subspacefield.org wrote:
Here's some behavior I've gathered through debugging. Note that all of the daemonization occurs before starting the reactor. If I don't close any file descriptors during daemonization, the program works fine. If I close all file descriptors (3, 4, 5, 6, 7) and above, it works, but upon disconnecting, I get the following error message: File "/etc/keeper_screen", line 675, in <module> reactor.run() File "/usr/local/lib/python2.5/site-packages/twisted/internet/base.py", line 1128, in run self.mainLoop() --- <exception caught here> --- File "/usr/local/lib/python2.5/site-packages/twisted/internet/base.py", line 1140, in mainLoop self.doIteration(t) File "/usr/local/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 126, in doSelect self._preenDescriptors() File "/usr/local/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 88, in _preenDescriptors self._disconnectSelectable(selectable, e, False) File "/usr/local/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 196, in _disconnectSelectable selectable.connectionLost(failure.Failure(why)) File "/usr/local/lib/python2.5/site-packages/twisted/internet/posixbase.py", line 150, in connectionLost os.close(fd) exceptions.OSError: [Errno 9] Bad file descriptor If I close all file descriptors two and above, I get that error message immediately. If I close all file descriptors eight and above, the program works normally. Is there some code in Twisted that expects certain file descriptors to be open? Anyone got a clue what could be happening here? -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sat, 30 May 2009 18:36:16 -0500, travis+ml-twisted@subspacefield.org wrote:
Creating the reactor at all may create a file descriptor which is necessary for its operation. Closing these will very likely cause problems. Also, Twisted has daemonization features already which are known to work well. ;) Why aren't you using these? Jean-Paul
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 08:26:40PM -0400, Jean-Paul Calderone wrote:
Creating the reactor at all may create a file descriptor which is necessary for its operation. Closing these will very likely cause problems.
This is all before calling any Twisted routines
Also, Twisted has daemonization features already which are known to work well. ;) Why aren't you using these?
Hadn't heard about it. Where's the documentation? -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
![](https://secure.gravatar.com/avatar/3f8b0ee4e56cebdb9430ed8d08e7b7c0.jpg?s=120&d=mm&r=g)
travis+ml-twisted@subspacefield.org wrote:
look for "twistd": http://twistedmatrix.com/projects/core/documentation/man/twistd-man.html http://twistedmatrix.com/projects/core/documentation/howto/basics.html http://twistedmatrix.com/projects/core/documentation/howto/tap.html http://twistedmatrix.com/projects/core/documentation/howto/application.html Johann
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Sun, 31 May 2009 15:18:50 -0500, travis+ml-twisted@subspacefield.org wrote:
It's pretty easily to unintentionally import and instantiate the default reactor. Are you certain daemonization all happens before that?
It's a feature of twistd. http://twistedmatrix.com/projects/core/documentation/howto/tap.html and http://twistedmatrix.com/projects/core/documentation/howto/application.html talk about how to structure your code so as to be able to take advantage of the features of twistd. Jean-Paul
![](https://secure.gravatar.com/avatar/66e8b2bf79acf5c0cc1885b04444a098.jpg?s=120&d=mm&r=g)
On Sat, May 30, 2009 at 06:17:54PM -0500, travis+ml-twisted@subspacefield.org wrote:
I ran ktrace/kdump against the binary and it appears that python likes to open file handles to the script and certain modules for file descriptors 5-7 or so (depending on how much you import), and if you close those file descriptors, unusual things happen when python tries to access them. In the end I decided it was not important enough to close all the parent's file descriptors, and it runs fine as a deamon leaving them open. -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email john@subspacefield.org to get blacklisted.
participants (3)
-
Jean-Paul Calderone
-
Johann Borck
-
travis+ml-twisted@subspacefield.org