Re: reuse of socket handles
From: Mike McMahon (MikeMcMahon_at_discussions.microsoft.com)
Date: 01/28/05
- Next message: Ben Hutchings: "Re: Reasons for GetThreadContext failure"
- Previous message: Arkady Frenkel: "Re: reuse of socket handles"
- In reply to: Alexander Grigoriev: "Re: reuse of socket handles"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 28 Jan 2005 02:55:03 -0800
Interesting idea, but I don't have memory access to the handle, as such.
I have to call a function to get it, and then call another function
to set it (to null, for example).
Thanks
Mike.
"Alexander Grigoriev" wrote:
> Lets say, you keep a socket handle in some structure member:
>
> pContext->hSocket
>
> When you want to close it:
>
> HANDLE ToClose = InterlockedExchange( & pContext->hSocket, NULL);
> closesocket(ToClose);
>
> This way, all other users of pContext will get a NULL socket. There is still
> a very short window for a race condition.
>
> But the cleanest solution is: only the thread that uses the socket should
> close it.
>
> "Mike McMahon" <MikeMcMahon@discussions.microsoft.com> wrote in message
> news:C282A6DD-6F16-4C5F-A330-30D9E04EB1A4@microsoft.com...
> > Yes. You are right. So let me explain the scenario.
> >
> > Say you have two threads. One is performing blocking recvfrom() calls on
> > the
> > socket and the second one closes the socket. There is a window
> > between when the first thread acquires the socket handle and when
> > it calls recvfrom(), during which the socket could be closed and
> > the handle allocated to some other newly created socket. When the
> > first thread calls recvfrom() it will be operating on the new socket
> > and it has no way to know.
> >
> > Say you try to protect the socket with a mutex and all threads
> > using the socket need to hold the mutex, then it is no longer
> > possible to pre-empt blocking calls like recvfrom() with closesocket()
> > (called from a different thread).
> >
> > So maybe I could add the following pieces to the question:
> >
> > - is there some way to close a socket without releasing the handle ?
> >
> > - is there some other way to make this work?
> >
> > Thanks
> > Mike.
> >
> > "Gabriel Bogdan" wrote:
> >> You should desing your code to work corectly even if this happens.
> >> And, there is no way to guarantee it won't happen no matter what you do
> >> to
> >> the sistem.
> >>
> >>
> >> "Mike McMahon" <MikeMcMahon@discussions.microsoft.com> wrote in message
> >> news:B1DC1D73-0505-4F64-8A4A-2BA2B3C03215@microsoft.com...
> >> > We have run into a problem which we think may be caused
> >> > by socket handles being recycled, by which I mean when
> >> > you close a socket, and create a new one around the same time,
> >> > the new socket gets the same handle as the old one.
> >> >
> >> > My question is if there is some way of tweaking the system
> >> > to increase the number of handles, or modifying the handle allocation
> >> behavior
> >> > in some way to reduce the probability of handles being recycled like
> >> > this?
> >> >
> >> > The OS would be Windows 2000, or XP.
> >> >
> >> > Thanks
> >> > Mike.
> >>
> >>
> >>
>
>
>
- Next message: Ben Hutchings: "Re: Reasons for GetThreadContext failure"
- Previous message: Arkady Frenkel: "Re: reuse of socket handles"
- In reply to: Alexander Grigoriev: "Re: reuse of socket handles"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|