TCP connection hanging in CLOSE_WAIT when using Invoke-RestMethod with PUT or DELETE - by vchrisb

Status : 


Sign in
to vote
ID 836732 Comments
Status Active Workarounds
Type Bug Repros 13
Opened 3/19/2014 7:05:50 AM
Access Restriction Public


When issuing more than two Invoke-RestMethod commands with the PUT or DELETE method against the same server, the third one fails with error: "Invoke-RestMethod : The operation has timed out." Only after closing the powershell and reopening will allow a new Invoke-RestMethod command run successfully.
This behavior was tested with two different servers, offering a REST API. GET and POST methods are not affected.

Using "netstat -an" you will see for the server communicating with, two active connections with state CLOSE_WAIT. On the server side you will see FIN_WAIT_2.
Investigating the tcp communication with wireshark, I can see that the client receives the "ACK,FIN" but is probably not issuing a close() as there is no "ACK,FIN" send as a response by the client to the server.
If using Fiddler in between you will not encounter the timeout but see in netstat for each Invoke-RestMethod command run, a CLOSE_WAIT. In the http standard only two connections are allowed to the same server. Fiddler is modifying this to allow unlimited http connections to the same server.

When closing the powershell a "RST" is send to the server, discarding the tcp session in CLOSE_WAIT state.
Sign in to post a comment.
Posted by SW Jeff Peterson on 9/3/2015 at 11:19 AM
Fails on third try:
Invoke-RestMethod -Method PATCH -Uri "" -Headers ...

Workaround appears to work:
Invoke-WebRequest -Method PATCH -Uri "" -Headers ...

Thank You
Posted by ukbd on 6/23/2015 at 5:54 AM
I should have mentioned we used as the client architecture rather than inventing our own. Hope someone finds this useful.
Posted by ukbd on 6/23/2015 at 5:51 AM
We see the same from Powershell 3 to our WebApi back end hosted in IIS. We don't see this when using .NET API's from C# and our workaround / solution was to create a Powershell C# Module that exposed the equivalent. Would be great if Microsoft actually fix this in a Powershell update though.
Posted by Jon Sutton on 3/18/2015 at 10:51 AM
Also seeing this behaviour.

Workaround suggested worked for me, thanks!
Posted by Mr. Ausnadian on 3/6/2015 at 10:15 PM
Seeing this issue also, even when using the workaround suggested.
Posted by DisplayThisBlahBlah on 2/19/2015 at 12:01 PM
I am also seeing this behavior.
Posted by Allan Boll on 11/5/2014 at 2:05 PM
Same for POSTs it seems. I got the same behaviour (includnig the close_wait in netstat) with the following:
Invoke-RestMethod '' -Method 'Post' -ContentType 'application/json' -Body '{"name": "value"}'