From: Eden Kirin on
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(a)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(a)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(a)sunce:~/data/project/ScgiServer/src> python test.py
Starting server.
^CException!
Terminating server, attempt 1.
Check n: 2.
eden(a)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.

--
www.vikendi.net -/- www.supergrupa.com
From: Eden Kirin on
Anyone?

--
www.vikendi.net -/- www.supergrupa.com
From: Diez B. Roggisch on
Eden Kirin wrote:

> 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(a)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(a)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(a)sunce:~/data/project/ScgiServer/src> python test.py
> Starting server.
> ^CException!
> Terminating server, attempt 1.
> Check n: 2.
> eden(a)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
From: Eden Kirin on
Diez B. Roggisch wrote:

>> 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.

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(a)sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
^Ceden(a)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.

--
www.vikendi.net -/- www.supergrupa.com
From: Diez B. Roggisch on
Eden Kirin wrote:

> Diez B. Roggisch wrote:
>
>>> 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.
>
> 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(a)sunce:~/data/project/ScgiServer/test> python test.py 1> output.txt
> ^Ceden(a)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