2009-07-28 08:36:30

by John Freeman

[permalink] [raw]
Subject: Re: Intermittent "Address already in use" error

Brad Midgley wrote:
> John,
>
>> From time to time, I encounter an "Address already in use" error when
>> attempting to bind a socket to a particular local adapter. The python
>> code looks like:
>>
>> Listener = BluetoothSocket(SCO)
>> Listener.bind((addr1, ))
>> Listener.listen(1)
>
> Is the error appearing when you want to rebind a short time after it
> was used? Maybe you need to setsockopt SO_REUSEADDR just like you
> would if you wanted to quickly rebind a tcp serversocket.

Brad,

Thanks for the suggestion. At first sight, that seemed like a possible cause.
However, further testing shows that I can make another attempt a few seconds
(limited by the speed of my fingers) after the socket has been closed, and that
it is usually successful.

When it fails, /sys/class/bluetooth/sco contains:

A2:C7:07:83:15:00 00:00:00:00:00:00 4

where "A2:C7:07:83:15:00" matches addr1 in the code above. Normally, content
appears in /sys/class/bluetooth/sco at the point of bind(), is modified slightly
by listen(), then disappears on close of the Listener socket.

Once it gets into this state, all further attempts to bind fail with "Address
already in use" ... until I reboot.

I haven't been able to pin down the exact circumstances that trigger the problem
other than that it seems more likely to occur following a program run that fails
with an unhandled exception. That is, it seems like things aren't cleaned-up
correctly sometimes. Unfortunately, I can't make it happen at will.

Any more ideas?

John.




2009-07-28 17:31:51

by Brad Midgley

[permalink] [raw]
Subject: Re: Intermittent "Address already in use" error

John

> Once it gets into this state, all further attempts to bind fail with
> "Address already in use" ... until I reboot.

if you're using kernel modules, you might be able to unload/reload a
bluetooth related kernel module to restore it. This would also help
narrowing down the cause.

--
Brad Midgley