Closed Bug 1242464 Opened 8 years ago Closed 8 years ago

Call shutdown before closesocket and use "hard" close on Windows

Categories

(Core :: Networking, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla47
Tracking Status
firefox47 --- fixed

People

(Reporter: dragana, Assigned: dragana)

Details

(Whiteboard: [necko-active])

Attachments

(2 files, 3 obsolete files)

We will call shutdown(socket) before calling closesocket.

We will also set Linger to hard close:
setting l_onoff to non zero and l_linger to zero:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms737582%28v=vs.85%29.aspx

https://msdn.microsoft.com/en-us/library/ms739165.aspx
obviously nspr will eventually need to be updated, but that's a pita. Do you think we could have a hacky ifdef version of this in nssockettransport to validate it?
(In reply to Patrick McManus [:mcmanus] from comment #2)
> obviously nspr will eventually need to be updated, but that's a pita. Do you
> think we could have a hacky ifdef version of this in nssockettransport to
> validate it?

setoption probably can be done outside nspr, but shutdown will be really hacky, I think adding a layer above PR_NSPR_IO_LAYER could work
I think you can probably skip the layer and just do  PROsfd osfd = PR_FileDesc2NativeHandle(fd); in the hacky bit :)
(In reply to Patrick McManus [:mcmanus] from comment #4)
> I think you can probably skip the layer and just do  PROsfd osfd =
> PR_FileDesc2NativeHandle(fd); in the hacky bit :)

I thought about that, but I need to ask security layer is not going to attempt writing to the socket? Actually the file descriptor still exist, but it will just get an error, so that is safe ...
you're right that it might get in the way of a clean tls shutdown..

I guess the layer can be inserted right before the close is done tho..
Attached patch bug_1242464_v2.patch (obsolete) — Splinter Review
Attachment #8711872 - Flags: feedback?(mcmanus)
Comment on attachment 8711872 [details] [diff] [review]
bug_1242464_v2.patch

Review of attachment 8711872 [details] [diff] [review]:
-----------------------------------------------------------------

::: netwerk/base/ShutdownLayer.cpp
@@ +16,5 @@
> +
> +PRStatus
> +WinSockClose(PRFileDesc *aFd)
> +{
> +  PROsfd osfd = PR_FileDesc2NativeHandle(aFd);

can we check the return value?

@@ +18,5 @@
> +WinSockClose(PRFileDesc *aFd)
> +{
> +  PROsfd osfd = PR_FileDesc2NativeHandle(aFd);
> +  shutdown(osfd, SD_BOTH);
> +  return PR_SUCCESS;

still need to call lower->close, right?

::: netwerk/base/nsSocketTransport2.cpp
@@ +1360,5 @@
>  
> +#if defined(XP_WIN)
> +    // Turn the linger option on an set the interval to 0. This will cause hard
> +    // close of the socket.
> +    opt.option =  PR_SockOpt_Linger;

I haven't read up on the actual bytes involved.. is this the same as DONT_LINGER referenced by one article?
Attachment #8711872 - Flags: feedback?(mcmanus) → feedback+
> >  
> > +#if defined(XP_WIN)
> > +    // Turn the linger option on an set the interval to 0. This will cause hard
> > +    // close of the socket.
> > +    opt.option =  PR_SockOpt_Linger;
> 
> I haven't read up on the actual bytes involved.. is this the same as
> DONT_LINGER referenced by one article?

SO_DONTLINGER only manipulate l_onoff parameter but not l_linder. So if you use SO_DONTLINGER to set l_onoff to 1 l_linder is implementation dependent.

We want l_onoff to be nonzero and l_linder to be 0.

https://msdn.microsoft.com/en-us/library/ms739165.aspx
Attached patch bug_1242464_v2.patch (obsolete) — Splinter Review
Attachment #8711872 - Attachment is obsolete: true
Attachment #8712082 - Flags: review?(mcmanus)
Comment on attachment 8712082 [details] [diff] [review]
bug_1242464_v2.patch

Review of attachment 8712082 [details] [diff] [review]:
-----------------------------------------------------------------

I would want to see at least a hand test of a try build that exercises this code to make sure it doesn't somehow turn into "linger for some amount of time". The definitions seem so overlapping to me. do you think that's a good idea?

possibly - a local service that accepts a connection and just sits there not reading anything.

get firefox to connect and POST a few MB.. should sit in socket buffers after rwin is exhausted. call this close path in socket transport and measure it with a timestamp and a log.

::: netwerk/base/nsSocketTransport2.cpp
@@ +1359,5 @@
>      }
>  
> +#if defined(XP_WIN)
> +    // Turn the linger option on an set the interval to 0. This will cause hard
> +    // close of the socket.

I would even write more about how the default state is a little different. and include the link to https://msdn.microsoft.com/en-us/library/ms739165.aspx - that was helpful to me and this is a non intuitive bit of code
Attachment #8712082 - Flags: review?(mcmanus) → review+
Attached patch bug_1242464_v2.patch (obsolete) — Splinter Review
rebased.
Attachment #8712082 - Attachment is obsolete: true
Attachment #8715773 - Flags: review+
comment updated.
Attachment #8715773 - Attachment is obsolete: true
Attachment #8715777 - Flags: review+
(In reply to Patrick McManus [:mcmanus] from comment #14)
> Comment on attachment 8712082 [details] [diff] [review]
> bug_1242464_v2.patch
> 
> Review of attachment 8712082 [details] [diff] [review]:
> -----------------------------------------------------------------
> 
> I would want to see at least a hand test of a try build that exercises this
> code to make sure it doesn't somehow turn into "linger for some amount of
> time". The definitions seem so overlapping to me. do you think that's a good
> idea?
> 
> possibly - a local service that accepts a connection and just sits there not
> reading anything.
> 
> get firefox to connect and POST a few MB.. should sit in socket buffers
> after rwin is exhausted. call this close path in socket transport and
> measure it with a timestamp and a log.
> 


I tried this did not work. I also tried the release version on the same computer, but i could not make socket buffer grow.
In test PR_Close always return immediately (0ms).
Whiteboard: [necko-active]
Keywords: checkin-needed
https://hg.mozilla.org/mozilla-central/rev/b973ffb1e642
Status: ASSIGNED → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla47
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: