473,756 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange socket problem

huy
Hi,

I'm using cherrypy to provide a user interface for users to start a
linux server program eg. os.system("nohu p myserver.py &"). The problem
is that if I stop cherrypy server and restart, I get the "Address
Already In Use" problem until I stop myserver.py. Can someone shed some
light on why this happens ? Why would the socket be held up in the other
process ?

Thanks

Huy
Jul 19 '05 #1
5 1586
When using os.system(), files that are open in the parent are available
in the child, as you can see here in Linux' listing of the files open by
the child program:

[jepler@sofa jepler]$ python -c 'f = open("/tmp/test", "w"); print f.fileno(); import os; os.system("ls -l /proc/self/fd")'
3
total 5
lrwx------ 1 jepler jepler 64 Jun 15 07:25 0 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 1 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 2 -> /dev/pts/2
l-wx------ 1 jepler jepler 64 Jun 15 07:25 3 -> /tmp/test
lr-x------ 1 jepler jepler 64 Jun 15 07:25 4 -> /proc/3108/fd

You may be able to set the FD_CLOEXEC flag on the files that should not
be passed to children, something like this:
old = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXE C)
Refer to a real Unix reference for more information on what FD_CLOEXEC
does.

You may want to replace the use of os.system() with fork + close files
+ exec. Then "myserver.p y" will no longer have the listening socket
descriptor of your cherrypy server.

Python 2.4's 'subprocess' module may work better in this respect than
os.system. It seems to include support for requesting that fds be
closed in the child (the close_fds parameter to subprocess.Pope n)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCsB/gJd01MZaTXX0RAr dnAJwN9iek350cq XtYDdUzzSomz6dQ IgCdFaC5
Ebd2XyIMsS+QgoL QB5jyPMU=
=9XcR
-----END PGP SIGNATURE-----

Jul 19 '05 #2
huy wrote:
Hi,

I'm using cherrypy to provide a user interface for users to start a
linux server program eg. os.system("nohu p myserver.py &"). The problem
is that if I stop cherrypy server and restart, I get the "Address
Already In Use" problem until I stop myserver.py. Can someone shed some
light on why this happens ? Why would the socket be held up in the other
process ?
As I understand it, if a socket is closed by the server, the OS
will typically hold on to the socket for a while, in case the
client wants some packages resent etc. This might be a part of
your problem.

Andrew Dalke explained it much better to me when I ran into this
some time ago. Quoted below:
Magnus Lyckå wrote:
Why doesn't my socket
get released by the OS when I exit via my handle_error?


Hi Magnus,

I wrote about this at
http://www.dalkescientific.com/writi...ng_xmlrpc.html

The reason for it is described at
http://hea-www.harvard.edu/~fine/Tech/addrinuse.html

You can set the class variable "allow_reuse_ad dress = True" in
your derived ExitableSocketS erver to get the behaviour you
expect, at the expense of some problems mentioned in the
above URL.

Andrew
da***@dalkescie ntific.com

Jul 19 '05 #3
Gurus,

I am still doing my baby steps in the wonderful world of python (so
far, so good).
However, I am quite familiar with sockets. There is a socket option
called
SO_REUSEADDR that your server should call to fix this problem.

Jul 19 '05 #4
huy
Hi Jeff,

Thanks for your help. Although I haven't confirmed this, I think you
just hit my nail on the head. I thought os.system was like a totally
separate process though, i.e nothing is shared. not the usual fork()
call within the program.

Regards,

Huy

Jeff Epler wrote:
When using os.system(), files that are open in the parent are available
in the child, as you can see here in Linux' listing of the files open by
the child program:

[jepler@sofa jepler]$ python -c 'f = open("/tmp/test", "w"); print f.fileno(); import os; os.system("ls -l /proc/self/fd")'
3
total 5
lrwx------ 1 jepler jepler 64 Jun 15 07:25 0 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 1 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 2 -> /dev/pts/2
l-wx------ 1 jepler jepler 64 Jun 15 07:25 3 -> /tmp/test
lr-x------ 1 jepler jepler 64 Jun 15 07:25 4 -> /proc/3108/fd

You may be able to set the FD_CLOEXEC flag on the files that should not
be passed to children, something like this:
old = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXE C)
Refer to a real Unix reference for more information on what FD_CLOEXEC
does.

You may want to replace the use of os.system() with fork + close files
+ exec. Then "myserver.p y" will no longer have the listening socket
descriptor of your cherrypy server.

Python 2.4's 'subprocess' module may work better in this respect than
os.system. It seems to include support for requesting that fds be
closed in the child (the close_fds parameter to subprocess.Pope n)

Jeff

Jul 19 '05 #5
huy
Hi Jeff,

Thanks for your help. Although I haven't confirmed this, I think you
just hit my nail on the head. I thought os.system was like a totally
separate process though, i.e nothing is shared. not the usual fork()
call within the program.

Regards,

Huy

Jeff Epler wrote:
When using os.system(), files that are open in the parent are available
in the child, as you can see here in Linux' listing of the files open by
the child program:

[jepler@sofa jepler]$ python -c 'f = open("/tmp/test", "w"); print f.fileno(); import os; os.system("ls -l /proc/self/fd")'
3
total 5
lrwx------ 1 jepler jepler 64 Jun 15 07:25 0 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 1 -> /dev/pts/2
lrwx------ 1 jepler jepler 64 Jun 15 07:25 2 -> /dev/pts/2
l-wx------ 1 jepler jepler 64 Jun 15 07:25 3 -> /tmp/test
lr-x------ 1 jepler jepler 64 Jun 15 07:25 4 -> /proc/3108/fd

You may be able to set the FD_CLOEXEC flag on the files that should not
be passed to children, something like this:
old = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXE C)
Refer to a real Unix reference for more information on what FD_CLOEXEC
does.

You may want to replace the use of os.system() with fork + close files
+ exec. Then "myserver.p y" will no longer have the listening socket
descriptor of your cherrypy server.

Python 2.4's 'subprocess' module may work better in this respect than
os.system. It seems to include support for requesting that fds be
closed in the child (the close_fds parameter to subprocess.Pope n)

Jeff


Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3370
by: Jane Austine | last post by:
Running Python 2.3 on Win XP It seems like socket is working interdependently with subprocesses of the process which created socket. ------------------------------------ #the server side >>> import socket >>> s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) >>> s.bind(('localhost',9000))
0
1541
by: Grzegorz Kaczor | last post by:
Hello all, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or not, and the other thread serves already connected clients: performs a Socket.Select and then...
0
1227
by: jack | last post by:
Situation - server having memory growth issues cut out all the code except this bit, where a client is constantly sending message and this just receives them, does nothing, and then receives again. I had stuck in a GC.Collect() and GC.WaitForFinalizers() call in the code so I could more easily watch the gen2 heap size and see if it was steady or not. But if I put it in one place, it would be steady, in another it would grow!!?? (see...
8
2011
by: Skink | last post by:
Hi, I'm preparing a python server that sends java classes and resources to custom java class loader. In order to make it faster I don't want to use URLClassLoader that uses HTTP protocol 1.0 and for each class/resource creates own connection. Instead I'd like to use raw sockets with simple protocol: - class loader sends a line terminated with \n with resource to get - python server reads that line, gets the file and sends back an
5
11701
by: Terry | last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a "recvFrom()" call and other thread sends a UDP packet to some address, that if the machine on the other end isn't up, that the "recvFrom()" call should just continue blocking. Right? What I'm seeing is that when I send a packet to a particular address that is not responding, my "recvFrom()" call throws an exception. I get "An existing connection was forcibly...
0
1250
by: Grzegorz Kaczor | last post by:
Hello, I've got a VERY strange network problem with Win2k Server and .NET. I've got one central server (hub) getting raw binary data (files) from many locations. Both server and clients are written in C# The server is quite simple: two threads, one accepts new connections and decides whether the client is authenticated to send data or not, and the other thread serves already connected clients: performs a Socket.Select and then gets...
1
1924
by: liubojin | last post by:
Hi guys, I have got some strange system behaviors regarding i/o delays in socket in python, here's the details: I'm writing a web proxy like program that intercepts and redirects requests from clients to my tomcat server, this proxy is written in python, and i'm starting a new thread to handle (redirect) the requests whenever there's a new client connected to my proxy. Each handler thread will read the http request line from the client...
6
4655
by: White Spirit | last post by:
I have the following code to send a packet to a remote socket and receive a response in return: System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); locSocket.Bind (new IPEndPoint (locIP, 2126)); /*
8
3218
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: Whenever I declare the sockaddr_in structure inside the main, the debugger crashes at line X*, not being able to access argv parameters (see code below). It is very strange.. by only being there, sockaddr_in does not allow me to question argc...
0
9152
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9716
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3676
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.