Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting socket.bind() exception, but no actual error

Reply
Thread Tools

getting socket.bind() exception, but no actual error

 
 
Clarence Gardner
Guest
Posts: n/a
 
      11-01-2004
I've got a problem that I don't see how to program around. A socket server
that was running fine, today started getting an exception from the bind()
call (errno 22, Invalid argument) and yet the bind had actually worked.

This is apparent from this code: it prints the error message, but then
breaks out of the loop and the listen() and accept() calls work normally.

How can one handle a function that is both working and raising an
exception?

sockobj=socket(AF_INET,SOCK_STREAM)
while True:
try:
sockobj.bind(('',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen(5)
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      11-01-2004
Clarence Gardner wrote:
> I've got a problem that I don't see how to program around. A socket server
> that was running fine, today started getting an exception from the bind()
> call (errno 22, Invalid argument) and yet the bind had actually worked.
>
> This is apparent from this code: it prints the error message, but then
> breaks out of the loop and the listen() and accept() calls work normally.
>
> How can one handle a function that is both working and raising an
> exception?
>
> sockobj=socket(AF_INET,SOCK_STREAM)
> while True:
> try:
> sockobj.bind(('',4321))
> except socketerror,e:
> print e
> if str(e).find('in use') == -1:
> break
> print '.'
> time.sleep(1)
> sockobj.listen(5)


Is it possible you already have a process bound to that port, and that
process is handling the incoming connections?

Otherwise it sounds a little bizarre. In fact even my own attempt at
explaining doesn't ring true because on Win2k, at least, a second bind
will raise (10048, 'Address already in use'), and I get a similar error
on Linux. Rats.

I take it this code has been copied and pasted? For example, if the host
were a single space rather than an empty string /that/ might cause this
problem. Sorry I can't be more help.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
 
Reply With Quote
 
 
 
 
Clarence Gardner
Guest
Posts: n/a
 
      11-02-2004
Steve Holden <(E-Mail Removed)> wrote in message news:<RSxhd.2736$931.635@lakeread01>...
> Clarence Gardner wrote:
> > I've got a problem that I don't see how to program around. A socket server
> > that was running fine, today started getting an exception from the bind()
> > call (errno 22, Invalid argument) and yet the bind had actually worked.


> Is it possible you already have a process bound to that port, and that
> process is handling the incoming connections?
>
> Otherwise it sounds a little bizarre. In fact even my own attempt at
> explaining doesn't ring true because on Win2k, at least, a second bind
> will raise (10048, 'Address already in use'), and I get a similar error
> on Linux. Rats.

And the whole point of the test in the exception handling suite (checking
for "in use") is to repeat the bind until that's not the case. This is, of
course, in a program in development which sometimes is not able to be
restarted right away after a problem.

> I take it this code has been copied and pasted? For example, if the host

Yep, that's the actual code. Of course, in this actual case, the error it's
getting has no bad effect, because there's no actual problem (and subsequent
operations on that socket proceed normally).

But it remains an odd problem. Clearly from a programming point of view,
one has to assume that an exception actually indicates a problem!
 
Reply With Quote
 
Michael Fuhr
Guest
Posts: n/a
 
      11-02-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Clarence Gardner) writes:

> And the whole point of the test in the exception handling suite (checking
> for "in use") is to repeat the bind until that's not the case. This is, of
> course, in a program in development which sometimes is not able to be
> restarted right away after a problem.


Why not? Because you get "Address already in use" exceptions due
to old connections still being in the TIME_WAIT state? If so, are
you aware that server processes should usually set the SO_REUSEADDR
socket option before calling bind()? Or is there some other reason
that bind() fails?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      11-03-2004
Michael Fuhr wrote:

> (E-Mail Removed) (Clarence Gardner) writes:
>
>
>>And the whole point of the test in the exception handling suite (checking
>>for "in use") is to repeat the bind until that's not the case. This is, of
>>course, in a program in development which sometimes is not able to be
>>restarted right away after a problem.

>
>
> Why not? Because you get "Address already in use" exceptions due
> to old connections still being in the TIME_WAIT state? If so, are
> you aware that server processes should usually set the SO_REUSEADDR
> socket option before calling bind()? Or is there some other reason
> that bind() fails?
>


There is some other reason: the exception argument is (errno 22, Invalid
argument), which is clearly not "Address in use".

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
 
Reply With Quote
 
Michael Fuhr
Guest
Posts: n/a
 
      11-03-2004
Steve Holden <(E-Mail Removed)> writes:

> Michael Fuhr wrote:
>
> > (E-Mail Removed) (Clarence Gardner) writes:
> >
> >>And the whole point of the test in the exception handling suite (checking
> >>for "in use") is to repeat the bind until that's not the case. This is, of
> >>course, in a program in development which sometimes is not able to be
> >>restarted right away after a problem.

> >
> > Why not? Because you get "Address already in use" exceptions due
> > to old connections still being in the TIME_WAIT state? If so, are
> > you aware that server processes should usually set the SO_REUSEADDR
> > socket option before calling bind()? Or is there some other reason
> > that bind() fails?

>
> There is some other reason: the exception argument is (errno 22, Invalid
> argument), which is clearly not "Address in use".


The retry loop likely isn't solving this problem but is rather
causing it. The code originally posted was:

sockobj=socket(AF_INET,SOCK_STREAM)
while True:
try:
sockobj.bind(('',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen(5)

Think about what happens if bind() succeeds: the code never breaks
out of the while loop, so it attempts to call bind() again. On
most systems that will cause the second call to bind() to fail with
EINVAL, or "Invalid argument". You could break out of the loop
when bind() succeeds, but the loop should be unnecessary if you set
the SO_REUSEADDR socket option before calling bind(), like this:

sockobj.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

Any decent network programming book (e.g., _UNIX Network Programming_,
Vol 1, by W. Richard Stevens) will explain that servers should set
SO_REUSEADDR before calling bind().

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
 
Reply With Quote
 
Clarence Gardner
Guest
Posts: n/a
 
      11-03-2004
(E-Mail Removed) (Michael Fuhr) wrote in message news:<4187d4fc$(E-Mail Removed)>...
? Because you get "Address already in use" exceptions due
> to old connections still being in the TIME_WAIT state? If so, are
> you aware that server processes should usually set the SO_REUSEADDR
> socket option before calling bind()? Or is there some other reason
> that bind() fails?


No, that's the only issue. I thought using that option was frowned upon,
but it's also been a couple of years since I needed to think about such
things, and I was too lazy to look it up for now.
 
Reply With Quote
 
Michael Fuhr
Guest
Posts: n/a
 
      11-03-2004
(E-Mail Removed) (Clarence Gardner) writes:

> (E-Mail Removed) (Michael Fuhr) wrote in message news:<4187d4fc$(E-Mail Removed)>...
> ? Because you get "Address already in use" exceptions due
> > to old connections still being in the TIME_WAIT state? If so, are
> > you aware that server processes should usually set the SO_REUSEADDR
> > socket option before calling bind()? Or is there some other reason
> > that bind() fails?

>
> No, that's the only issue. I thought using that option was frowned upon,
> but it's also been a couple of years since I needed to think about such
> things, and I was too lazy to look it up for now.


SO_REUSEADDR is hardly frowned upon -- quite the contrary: it's
recommended. See for example the socket bible, _UNIX Network
Programming_, Vol 1, by W. Richard Stevens. On p 207 (2nd edition),
in the summary for the Socket Options chapter, Stevens says:

The most commonly used options that we might encounter are
SO_KEEPALIVE, SO_RCVBUF, SO_SNDBUF, and SO_REUSEADDR. The
latter should always be set for a TCP server before it calls
bind.

Earlier in the chapter, on pp 194-197, Stevens discusses SO_REUSEADDR
in detail and says that "_All_ TCP servers should specify this
socket option to allow the server to be restarted in this situation"
(emphasis his; page numbers may differ in the 3rd edition).

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
 
Reply With Quote
 
redBind redBind is offline
Junior Member
Join Date: Apr 2007
Posts: 1
 
      04-25-2007
Quote:
Originally Posted by Clarence Gardner
I've got a problem that I don't see how to program around. A socket server
that was running fine, today started getting an exception from the bind()
call (errno 22, Invalid argument) and yet the bind had actually worked.

This is apparent from this code: it prints the error message, but then
breaks out of the loop and the listen() and accept() calls work normally.

How can one handle a function that is both working and raising an
exception?

sockobj=socket(AF_INET,SOCK_STREAM)
while True:
try:
sockobj.bind(('',4321))
except socketerror,e:
print e
if str(e).find('in use') == -1:
break
print '.'
time.sleep(1)
sockobj.listen(5)

hey guys i'm kind of a newbie here.. just like to ask questions... this problem is currently my problem but i don't know if it were different when it comes to what server the problem arose... mine actually happened in solaris 10 (november 2006 release) and it only happens when the address is Ipv6 in SCTP connection... i hope you could help me out


thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the actual size of a sparse file Daniel Berger Ruby 9 01-06-2011 09:45 PM
Getting actual width of span element August Karlstrom Javascript 7 04-02-2010 05:35 PM
getting actual url of frames using javascript devloper Javascript 0 09-07-2007 02:06 PM
Getting the actual message being transmitted Grigoris D ASP .Net Web Services 1 11-15-2004 09:40 PM



Advertisments