Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PicklingError on Ubuntu #79

Closed
pgjones opened this issue Sep 6, 2022 · 7 comments
Closed

PicklingError on Ubuntu #79

pgjones opened this issue Sep 6, 2022 · 7 comments

Comments

@pgjones
Copy link
Owner

pgjones commented Sep 6, 2022

Traceback (most recent call last):
File "/layers/[google.python.pip/pip/bin/hypercorn](http://google.python.pip/pip/bin/hypercorn)", line 8, in <module>
sys.exit(main())
File "/layers/[google.python.pip/pip/lib/python3.9/site-packages/hypercorn/](http://google.python.pip/pip/lib/python3.9/site-packages/hypercorn/)main.py", line 287, in main
run(config)
File "/layers/[google.python.pip/pip/lib/python3.9/site-packages/hypercorn/run.py](http://google.python.pip/pip/lib/python3.9/site-packages/hypercorn/run.py)", line 53, in run
processes = start_processes(config, worker_func, sockets, shutdown_event, ctx)
File "/layers/[google.python.pip/pip/lib/python3.9/site-packages/hypercorn/run.py](http://google.python.pip/pip/lib/python3.9/site-packages/hypercorn/run.py)", line 95, in start_processes
process.start()
File "/opt/python3.9/lib/python3.9/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/opt/python3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
return Popen(process_obj)
File "/opt/python3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in init
super().init(process_obj)
File "/opt/python3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in init
self._launch(process_obj)
File "/opt/python3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 47, in _launch
reduction.dump(process_obj, fp)
File "/opt/python3.9/lib/python3.9/multiprocessing/reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function getAvailableMemory at 0x3e3cacbe7550>: import of module 'module.name' failed
@ThomasChiroux
Copy link

ThomasChiroux commented Sep 7, 2022

Hi ! same here using hypercorn 0.14.3 for fastapi - debian docker image - python 3.10.4-slim-bullseye

Traceback (most recent call last):
  File "/usr/local/bin/hypercorn", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/hypercorn/__main__.py", line 287, in main
    run(config)
  File "/usr/local/lib/python3.10/site-packages/hypercorn/run.py", line 53, in run
    processes = start_processes(config, worker_func, sockets, shutdown_event, ctx)
  File "/usr/local/lib/python3.10/site-packages/hypercorn/run.py", line 95, in start_processes
    process.start()
  File "/usr/local/lib/python3.10/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/usr/local/lib/python3.10/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/usr/local/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/local/lib/python3.10/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/local/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/usr/local/lib/python3.10/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function booleanize at 0x7f5527466f80>: import of module 'module.name' failed

In my case the problem is located in my base docker image, derived from https://github.com/bynect/hypercorn-fastapi-docker - in particular the hypercorn.py file : https://github.com/bynect/hypercorn-fastapi-docker/blob/main/images/hypercorn_conf.py

it's certainly a side effet of the new 0.14 release.
I did not have the time to dig that particular problem, so I re-downgraded to 0.13.2 which is ok

@benoit9126
Copy link

In order to patch this problem, I remove from the hypercorn.conf.py every objects embedding a not pickable object. In my case, it was a rich console for a logging handler.

@pgjones
Copy link
Owner Author

pgjones commented Sep 8, 2022

That would make sense, the config is sent to the processes by being pickled. @ThomasChiroux do you also customise the config?

@ThomasChiroux
Copy link

yes, my configuration file is almost exactly like this one:

https://github.com/bynect/hypercorn-fastapi-docker/blob/main/images/hypercorn_conf.py

which is used here:

https://github.com/bynect/hypercorn-fastapi-docker/blob/main/images/start.sh

the conf file is a .py file like this pseudo code below:

exec hypercorn -c  hypercorn_conf.py "$APP_MODULE"

($HYPERCORN_CONF == hypercorn_conf.py)

hyperconf.py uses a custom method to map bools from env:

def booleanize(value) -> bool:
    if value is None:
        return False
    
    falsy = ["no", "n", "0", "false"]
    truly = ["yes", "y", "1", "true"]

    if value.lower() in falsy:
        return False
    elif value.lower() in truly:
        return True
    else:
        raise TypeError("Non boolean-like value {}".format(value))


#ssl opts
use_ssl = booleanize(os.getenv("USE_SSL", "False"))

(the method booleanize is the one not serializable)

then at the end of the .py file after setting all the variables the conf is outputed for hypercorn:

conf_data = {
    "accesslog": accesslog,
    "errorlog": errorlog,
    "loglevel": loglevel,
    "backlog": backlog,
    "bind": bind,
    "insecure_bind": insecure_bind if use_tcp and use_ssl else None,
    "quic_bind": quic_bind if use_quic_bind else None,
    "graceful_timeout": graceful_timeout,
    "keep_alive_timeout": keep_alive_timeout,
    "workers": workers,
    "worker_class": worker_class,
    "env": {
        "host": host,
        "ssl_port": ssl_port if use_ssl else None,
        "tcp_port": tcp_port if use_tcp else None,
        "use_ssl": use_ssl,
        "use_tcp": use_tcp,        
        "workers_multiplier": workers_multiplier,
        "cores": cores
    }
}

print(json.dumps(conf_data, indent = 4), flush = True)

@benoit9126
Copy link

@ThomasChiroux Can you try to move the function booleanize outside the hypercorn configuration file? E.g. in a utils.py file aside the hypercorn_conf.py file? In your configuration file, add from utils import booleanize. It seems to work for me.

@ThomasChiroux
Copy link

I managed to make it work by removing booleanize entirely. It was not widely used in the module.

I'm now testing the new image with the last hypercorn, seems ok

thks.
Thomas

@pgjones
Copy link
Owner Author

pgjones commented Sep 14, 2022

I've tried to make the error clearer in 4645aa0.

@pgjones pgjones closed this as completed Sep 14, 2022
asuka4624254 pushed a commit to asuka4624254/hypercorn-fastapi-docker that referenced this issue Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants