bytecodealliance / componentize-py

Apache License 2.0
158 stars 19 forks source link

[bug] zlib support required by widely used python modules #96

Open c-x opened 5 months ago

c-x commented 5 months ago

Hi!

Apparently cpython was compiled without zlib support, which is required by the requests library (probably the most used/famous http library in python).

% poetry run componentize-py --wit-path myapp/wit --world plugins componentize -p myapp main -o my-app.wasm
Traceback (most recent call last):
  File "/myapp/python/.venv/bin/componentize-py", line 8, in <module>
    sys.exit(script())
             ^^^^^^^^
AssertionError: Traceback (most recent call last):
  File "/0/main.py", line 2, in <module>
    import requests
  File "/1/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/1/urllib3/__init__.py", line 18, in <module>
    from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
  File "/1/urllib3/connectionpool.py", line 15, in <module>
    from ._request_methods import RequestMethods
  File "/1/urllib3/_request_methods.py", line 10, in <module>
    from .response import BaseHTTPResponse
  File "/1/urllib3/response.py", line 11, in <module>
    import zlib
ModuleNotFoundError: No module named 'zlib'

Caused by:
    ModuleNotFoundError: No module named 'zlib'

Issue probably linked to https://github.com/bytecodealliance/componentize-py/issues/64

dicej commented 5 months ago

I believe @brettcannon is planning to add zlib to the WASI build of CPython, along with other native modules such as bzip2, etc.

Regarding requests: I agree that this would be a great library to support, but it's going to take more than adding zlib to support it. I assume it also uses the ssl module, which is going to be way tougher to support given that Wasm doesn't currently have constant time operations, which means OpenSSL is vulnerable to timing attacks when compiled to Wasm. I think the most practical way to support requests is to port it to use wasi:http instead of sockets on WASIp2, which someone would need to volunteer to do.

brettcannon commented 5 months ago

I believe @brettcannon is planning to add zlib to the WASI build of CPython, along with other native modules such as bzip2, etc.

Correct, it's planned: https://opensource.snarky.ca/Python/WASI#Plans .

c-x commented 5 months ago

@brettcannon @dicej do you have any ETA/objective for this fix?

brettcannon commented 4 months ago

@c-x No ETA from me. The objective is to get zlib statically compiled into the binary I build for CPython via https://github.com/python/cpython/blob/main/Tools/wasm/wasi.py .