[Ironpython-users] requests hang on get or post http request

Jeff Hardy jdhardy at gmail.com
Fri Aug 22 11:44:27 CEST 2014


On Thu, Aug 21, 2014 at 5:37 PM, Daniel Fernandez
<fernandez_dan2 at hotmail.com> wrote:
> Hi All,
>
> I'm using the latest 2.7.5 b3 code and I wanted to see if requests works. I
> pulled the latest from github. It would hang on simple requests.get or
> requests.post. It would hang on the reading the http response.  I attached
> the debugger and traced it to PythonBinaryReader:Read method. in the
> PythonFile.cs  It hangs when size = 0. It hangs specifically in this loop
>
> int leftCount = size;
>  while (true) {
>                 int count = _stream.Read(data, offset, leftCount);
>                 if (count <= 0) break;
>                 leftCount -= count;
>                 if (leftCount <= 0) break;
>                 offset += count;
>             }
>
> It hangs on the _stream.read where offset is  0 and leftCount is zero and
> the stream is a NetworkStream.

That's very odd. What's the result of _stream.Read in this case (i.e.
what is count set to)? The docs for NetworkStream.Read[1] don't really
specify what should happen if size (leftCount in the code above) is 0.
If just says that it returns the number of bytes read, or 0 if there
is nothing left to read ... but what if we only ask for 0 bytes, and
there's still data left to read?

My instinct would be that it should return 0 and then break on the
next line, but that's clearly not happening. Also, why does it not
break on the `if (leftCount <= 0) break;` line?

Unless a size of 0 means for NetworkStream.Read to block and read
everything until the stream closes. But the NetworkStream docs don't
say that. They don't specify at all what happens when size is 0. And
the comments for PythonBinaryReader.Read don't say what should happen
when size = 0 either.

Playing with the CPython interpreter, though, calling f.read(0) always
returns an empty string (f.read(-1) is read-to-end), so that seems
like the behaviour. I don't know what's going on with
NetworkStream.Read (maybe a bug?) but IP should work around it.

[1] http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.read(v=vs.110).aspx

> I ran a quick experiment and just added one line, didn't want to change too
> much. I changed.
> int leftCount = size;
> while (true) {
>                 if (leftCount <= 0) break;
>                 int count = _stream.Read(data, offset, leftCount);
>                 if (count <= 0) break;
>                 leftCount -= count;
>                 if (leftCount <= 0) break;
>                 offset += count;
>             }
>
>

It would be better to move the check out of the loop and check
if(size==0) instead. That makes it clearer what case is being handled.
You could skip the whole thing and just do

  if(size == 0) {
    return string.Empty;
  }

at the start of the function.

> After this I was able to get all HTTP GETs and POSTs working. I did this
> with rest type calls and normal web transactions.
>
> I was just wanted to check if any ran into this issue before? I'm not sure
> if this was just a me thing.

I'd be curious to know which version of the .NET framework you're running on.

- Jeff


More information about the Ironpython-users mailing list