SCGIServer and unusal termination

E

Eden Kirin

Hi there,

I'm playing with SCGIServer
(http://vmlinux.org/cgi-bin/dwww/usr/share/doc/python-scgi/guide.html),
everything works just fine, but one thing bothers me. All prints after
try-except block are executed twice after the Ctrl+C is pressed!

test.py:
#-------------------------
from scgi.scgi_server import SCGIServer

n = 0
print "Starting server."

try:
SCGIServer().serve()
except (KeyboardInterrupt, SystemExit):
print "Exception!"

# print lines are executed twice (?!)
n += 1
print "Terminating server, attempt %d." % n
n += 1
print "Check n: %d." % n
#-------------------------

This is the output:

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Exception!
Terminating server, attempt 1.
Check n: 2.
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>


If I put something else in try-except block, code after is executed
normally:

try:
while 1:
pass
except (KeyboardInterrupt, SystemExit):
print "Exception!"

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>

Environment is 64bit Ubuntu with Python v2.6.4.

Is there some reasonable explanation for this behaviour? Thanks in advance.
 
D

Diez B. Roggisch

Eden said:
Hi there,

I'm playing with SCGIServer
(http://vmlinux.org/cgi-bin/dwww/usr/share/doc/python-scgi/guide.html),
everything works just fine, but one thing bothers me. All prints after
try-except block are executed twice after the Ctrl+C is pressed!

test.py:
#-------------------------
from scgi.scgi_server import SCGIServer

n = 0
print "Starting server."

try:
SCGIServer().serve()
except (KeyboardInterrupt, SystemExit):
print "Exception!"

# print lines are executed twice (?!)
n += 1
print "Terminating server, attempt %d." % n
n += 1
print "Check n: %d." % n
#-------------------------

This is the output:

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Exception!
Terminating server, attempt 1.
Check n: 2.
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>


If I put something else in try-except block, code after is executed
normally:

try:
while 1:
pass
except (KeyboardInterrupt, SystemExit):
print "Exception!"

eden@sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Terminating server, attempt 1.
Check n: 2.
eden@sunce:~/data/project/ScgiServer/src>

Environment is 64bit Ubuntu with Python v2.6.4.

Is there some reasonable explanation for this behaviour? Thanks in
advance.

I can only guess that SCGIServer does something to stdout. Your code isn't
executed twice, so the doubling seems to come from writing it twice.

Try e.g. redirecting stdout and stderr to different files, and see if things
appear once in both.

Diez
 
E

Eden Kirin

Diez said:
I can only guess that SCGIServer does something to stdout. Your code isn't
executed twice, so the doubling seems to come from writing it twice.

Yes I know that code isn't executed twice since the value of n remains
the same, only print lines are doubled.
Try e.g. redirecting stdout and stderr to different files, and see if things
appear once in both.

Redirection of stdout:

eden@sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
^Ceden@sunce:~/data/project/ScgiServer/test> cat output.txt
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.

Redirecting stderr creates an empty file. I still haven't found the
solution.
 
D

Diez B. Roggisch

Eden said:
Yes I know that code isn't executed twice since the value of n remains
the same, only print lines are doubled.


Redirection of stdout:

eden@sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
^Ceden@sunce:~/data/project/ScgiServer/test> cat output.txt
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.
Starting server.
Exception!
Terminating server, attempt 1.
Check n: 2.

Redirecting stderr creates an empty file. I still haven't found the
solution.

Then

- save a reference to sys.stdout *before* invoking the server
- compare to it after interruption. If it has changed, you at least know
that somebody messed with it, and can beat him or whatever you see fit.

Diez
 
E

Eden Kirin

Diez said:
- save a reference to sys.stdout *before* invoking the server
- compare to it after interruption. If it has changed, you at least know
that somebody messed with it, and can beat him or whatever you see fit.

Thanks for the help. Finally, I dropped python-scgi module and I wrote
my own SCGI server. There was not only the problem with stdout, but one
much serious which made it unusable to me. Every SCGI request was forked
as a new process, without the ability to access the shared globals
within the project.
 
Ð

Дамјан ГеоргиевÑки

everything works just fine, but one thing bothers me. All prints after
try-except block are executed twice after the Ctrl+C is pressed!

test.py:
#-------------------------
from scgi.scgi_server import SCGIServer

n = 0
print "Starting server."

try:
SCGIServer().serve()
except (KeyboardInterrupt, SystemExit):
print "Exception!"

# print lines are executed twice (?!)
n += 1
print "Terminating server, attempt %d." % n
n += 1
print "Check n: %d." % n
#-------------------------

SCGIServer().serve() forks, so it seems that there are 2 python
processes continuing to run after SCGIServer().serve()
 
E

Eden Kirin

Дамјан ГеоргиевÑки said:
SCGIServer().serve() forks, so it seems that there are 2 python
processes continuing to run after SCGIServer().serve()

I noticed that which makes it unusable to me. Also, it took me almost
whole day to realize this. I'm adopting a huge application to work with
SCGI which shares a certain amount of data between working threads and
SCGI handler. I couldn't realize the cause of erratic and unconsistent
data behaviour. After diving into python-scgi code, I gave it up and
wrote my own SCGI server.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top