Skip to content

New mmap strategy breaks consuming binary http streams #23

Description

@trifle

Hi, I noticed you've added mmap to the code, which is great!

Unfortunately, it breaks consuming http streams, because urllib3 raw streams (urllib3.response.HTTPResponse) also (surprisingly) have a fileno!

So we get an exception:

    in get_memoryview(data)
     98 # Handle file object opened in 'rb' mode
     99 if hasattr(data, "fileno"):
--> 100     mm = mmap.mmap(data.fileno(), 0, access=mmap.ACCESS_READ)
    101     return memoryview(mm)
    103 # Handle BufferedReader

OSError: [Errno 22] Invalid argument

I've checked the set difference of attributes of a binary file handle and a HTTPResponse, and these attributes are (on my macos machine) exclusively available on files:

['_dealloc_warn',
 '_finalizing',
 'detach',
 'mode',
 'name',
 'peek',
 'raw',
 'readinto1',
 'write']

So it may be worth either switching the check or alternatively adding an extra condition that prevents mmap on urllib3.response.HTTPResponse objects.

Cheers!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions