473,663 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

safely closing a socket?

I've been doing some socket programming with threads and am confused how
to safely shut down a connection.

First of all, using the file-like object returned by urllib to do ftp
file download (Python 2.3, MacOS X), I can hang the process by:
- using one thread to read the file-like object
- trying to close the connection in the main thread (while still
downloading)
it's not clear to me what's hung, but it just sits there. No major CPU
cycles being eaten. Some kind of deadlock, perhaps???

I've worked around the problem by setting a flag and having the read
thread quit. But this can take quite awhile. Is there some fast safe way
to terminate such a download?

Also, is this more generally an issue with sockets? Is it dangerous to
close a socket while another thread may be reading or writing it?

Any hints or pointers to information would be much appreciated.

-- Russell
Jul 18 '05 #1
1 2755
In article <ro************ *************@n ntp2.u.washingt on.edu>,
"Russell E. Owen" <ro***@cesmail. net> wrote:
I've been doing some socket programming with threads and am confused how
to safely shut down a connection.

First of all, using the file-like object returned by urllib to do ftp
file download (Python 2.3, MacOS X), I can hang the process by:
- using one thread to read the file-like object
- trying to close the connection in the main thread (while still
downloading)
it's not clear to me what's hung, but it just sits there. No major CPU
cycles being eaten. Some kind of deadlock, perhaps???

I've worked around the problem by setting a flag and having the read
thread quit. But this can take quite awhile. Is there some fast safe way
to terminate such a download?

Also, is this more generally an issue with sockets? Is it dangerous to
close a socket while another thread may be reading or writing it?


Greetings, Russell! It depends on the platform.

Casual experiment with two variations -

1. child thread in socketobject.re cv(), parent
calls socketobject.cl ose()

MacOS X: recv returns ''
FreeBSD 5.1: recv raises exception (errno EBADF)
Redhat Linux 7.2: recv continues

2. child thread in socketobject.ma kefile('r').rea d(),
parent calls socketobject.cl ose()

MacOS X: read continues
FreeBSD 5.1: read continues
Redhat Linux 7.2: read continues, program doesn't exit

So there seems to be an incentive to avoid the file object
thing, but you couldn't depend on that working on every
possible platform (e.g., Linux.)

One sort of deluxe option would be an inter-thread communications
channel, probably a pipe, that the child thread reads along
with its socket, via select(). It won't interrupt a recv
actually in progress, transferring data, but ordinarily the
intent is to abort it when it would be waiting for more data.
This doesn't work well with fileobject I/O, has to be system
level posix.read or socketobject.re cv.

Donn Cave, do**@u.washingt on.edu
Jul 18 '05 #2

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

Similar topics

8
9276
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the python wrapper module for sockets, and found that the close command doesn't actually call the underlying close! this didn't seem right, so i added it, and my code now works simply and as expected. def close(self):
4
2427
by: flupke | last post by:
Hi, I have a gui (made in wxPython) that enables a user to connect to a server and issue some commands. The problem occurs when i try to disconnect the client. It exits but it doesn't return to the prompt. I have to push Ctrl-C in order to have it exit completely. The GUI is closed though. This is a piece of code from the main class that connects to the server and starts a thread that handles the connection: =======================...
1
3787
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
0
1258
by: Shane | last post by:
I want to use BeginRead on a socket stream. I want to abort the read if no data arrives within a certain time interval. I know i can't cancell the BeginRead directly but closing the socket seems to work. Does this cause any problems (Resource leaks etc) ? Should I also call EndRead after closing the socket even though it will it throw ? Has anyone done anything similar ? Shane
3
2821
by: Tom Opgenorth | last post by:
I'm experiencing a problem with sockets, and I'm really hoping someone can help me, please and thank you. I've written a TCP Server, which listens on a port for an incoming connection. When the client connects, the connection is NEVER to be closed by the server. The client will send messages as necessary. After each message the server is to send an acknowledgement back to the client. That is the client's indicator to send the next...
2
3490
by: Lenard Gunda | last post by:
Hi, I have the following problem when I am working with sockets in C#. I do not remember running into the same problem some while ago, when working with sockets from C++ (using native code, not managed). I create a server socket, and then listen on it. I then accept a new connection from that server socket. If I now close the server socket, it also closes the accepted connection as well, automatically. This feels stupid.
4
2761
by: Funke | last post by:
Assume that in C#, I create a server socket (listener) and code to start new threads with each connection using BeginAccept(). After some time, I have three threads running, each with their own client socket connection. If I close the listener socket, will the client sockets also shut down? Or do I need to manually shut these down as well?
3
5261
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue is about Python or POSIX. In the following, the first time through, everything works. On the second connection, though, the same file descriptor as the first connection may be re-used, but for some reason, trying to do os.read/close on that...
4
16102
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open the socket again, the bind fails because the port is still in use. When I execute the code in "debug" mode, the problem never occurs. When I execute the same code in release mode, the problem appears about 20% of the time. Here's the code:
0
8858
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...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
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
8634
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...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
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
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.