SCGIServer and unusal termination

Discussion in 'Python' started by Eden Kirin, Nov 16, 2009.

  1. Eden Kirin

    Eden Kirin Guest

    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.

    --
    www.vikendi.net -/- www.supergrupa.com
    Eden Kirin, Nov 16, 2009
    #1
    1. Advertising

  2. Eden Kirin

    Eden Kirin Guest

    Eden Kirin, Nov 17, 2009
    #2
    1. Advertising

  3. 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@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
    Diez B. Roggisch, Nov 17, 2009
    #3
  4. Eden Kirin

    Eden Kirin Guest

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

    --
    www.vikendi.net -/- www.supergrupa.com
    Eden Kirin, Nov 17, 2009
    #4
  5. 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@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
    Diez B. Roggisch, Nov 17, 2009
    #5
  6. Eden Kirin

    Eden Kirin Guest

    Diez B. Roggisch wrote:

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

    --
    www.vikendi.net -/- www.supergrupa.com
    Eden Kirin, Nov 19, 2009
    #6
  7. > 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()


    --
    дамјан ((( http://damjan.softver.org.mk/ )))

    Spammers scratch here with a diamond to find my address:
    |||||||||||||||||||||||||||||||||||||||||||||||
    Дамјан ГеоргиевÑки, Nov 19, 2009
    #7
  8. Eden Kirin

    Eden Kirin Guest

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

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

    --
    www.vikendi.net -/- www.supergrupa.com
    Eden Kirin, Nov 19, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB
    Replies:
    3
    Views:
    21,951
  2. SuperSizeMe
    Replies:
    3
    Views:
    493
    SuperSizeMe
    Jan 14, 2005
  3. Scott Carlson

    program termination and return value

    Scott Carlson, Oct 8, 2004, in forum: Python
    Replies:
    1
    Views:
    293
    F. Petitjean
    Oct 8, 2004
  4. Gokul
    Replies:
    3
    Views:
    385
    Gokul
    Jul 28, 2007
  5. Aaron Sanders

    SSL Termination and ASP.NET 2.0

    Aaron Sanders, Oct 4, 2006, in forum: ASP .Net Security
    Replies:
    0
    Views:
    216
    Aaron Sanders
    Oct 4, 2006
Loading...

Share This Page