Skip to content

fast api crashing #183

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

Open
MohannadEhabBarakat opened this issue Apr 27, 2025 · 1 comment
Open

fast api crashing #183

MohannadEhabBarakat opened this issue Apr 27, 2025 · 1 comment

Comments

@MohannadEhabBarakat
Copy link

I'm trying to use your awesome work with fast api, but it crashes. If I import grequests first I get


╭──────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────╮
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/fastapi_cli/cli.py:245 in dev                            │
│                                                                                                                            │
│   242 │                                                                                                                    │
│   243 │   Otherwise, it uses the first [bold]FastAPI[/bold] app found in the imported module o                             │
│   244 │   """                                                                                                              │
│ ❱ 245 │   _run(                                                                                                            │
│   246 │   │   path=path,                                                                                                   │
│   247 │   │   host=host,                                                                                                   │
│   248 │   │   port=port,                                                                                                   │
│                                                                                                                            │
│ ╭────────────── locals ───────────────╮                                                                                    │
│ │           app = None                │                                                                                    │
│ │          host = '127.0.0.1'         │                                                                                    │
│ │          path = PosixPath('api.py') │                                                                                    │
│ │          port = 8000                │                                                                                    │
│ │ proxy_headers = True                │                                                                                    │
│ │        reload = True                │                                                                                    │
│ │     root_path = ''                  │                                                                                    │
│ ╰─────────────────────────────────────╯                                                                                    │
│                                                                                                                            │
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/fastapi_cli/cli.py:162 in _run                           │
│                                                                                                                            │
│   159 │   │   toolkit.print("Logs:")                                                                                       │
│   160 │   │   toolkit.print_line()                                                                                         │
│   161 │   │                                                                                                                │
│ ❱ 162 │   │   uvicorn.run(                                                                                                 │
│   163 │   │   │   app=import_string,                                                                                       │
│   164 │   │   │   host=host,                                                                                               │
│   165 │   │   │   port=port,                                                                                               │
│                                                                                                                            │
│ ╭───────────────────────────────────────── locals ──────────────────────────────────────────╮                              │
│ │           app = None                                                                      │                              │
│ │       command = 'dev'                                                                     │                              │
│ │          host = '127.0.0.1'                                                               │                              │
│ │   import_data = ImportData(                                                               │                              │
│ │                 │   app_name='app',                                                       │                              │
│ │                 │   module_data=ModuleData(                                               │                              │
│ │                 │   │   module_import_str='api',                                          │                              │
│ │                 │   │   extra_sys_path=PosixPath('/home/aiadmin/prop_recom_gemma'),       │                              │
│ │                 │   │   module_paths=[                                                    │                              │
│ │                 │   │   │   PosixPath('/home/aiadmin/prop_recom_gemma/api.py')            │                              │
│ │                 │   │   ]                                                                 │                              │
│ │                 │   ),                                                                    │                              │
│ │                 │   import_string='api:app'                                               │                              │
│ │                 )                                                                         │                              │
│ │ import_string = 'api:app'                                                                 │                              │
│ │   module_data = ModuleData(                                                               │                              │
│ │                 │   module_import_str='api',                                              │                              │
│ │                 │   extra_sys_path=PosixPath('/home/aiadmin/prop_recom_gemma'),           │                              │
│ │                 │   module_paths=[PosixPath('/home/aiadmin/prop_recom_gemma/api.py')]     │                              │
│ │                 )                                                                         │                              │
│ │          path = PosixPath('api.py')                                                       │                              │
│ │          port = 8000                                                                      │                              │
│ │ proxy_headers = True                                                                      │                              │
│ │        reload = True                                                                      │                              │
│ │     root_path = ''                                                                        │                              │
│ │     root_tree = <rich.tree.Tree object at 0x76e15dc552a0>                                 │                              │
│ │   server_type = 'development'                                                             │                              │
│ │       toolkit = <rich_toolkit.toolkit.RichToolkit object at 0x76e15dc55480>               │                              │
│ │           url = 'http://127.0.0.1:8000'                                                   │                              │
│ │      url_docs = 'http://127.0.0.1:8000/docs'                                              │                              │
│ │       workers = None                                                                      │                              │
│ ╰───────────────────────────────────────────────────────────────────────────────────────────╯                              │
│                                                                                                                            │
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/uvicorn/main.py:575 in run                               │
│                                                                                                                            │
│   572 │   try:                                                                                                             │
│   573 │   │   if config.should_reload:                                                                                     │
│   574 │   │   │   sock = config.bind_socket()                                                                              │
│ ❱ 575 │   │   │   ChangeReload(config, target=server.run, sockets=[sock]).run()                                            │
│   576 │   │   elif config.workers > 1:                                                                                     │
│   577 │   │   │   sock = config.bind_socket()                                                                              │
│   578 │   │   │   Multiprocess(config, target=server.run, sockets=[sock]).run()                                            │
│                                                                                                                            │
│ ╭──────────────────────────────────────────────────────── locals ────────────────────────────────────────────────────────╮ │
│ │                    access_log = True                                                                                   │ │
│ │                           app = 'api:app'                                                                              │ │
│ │                       app_dir = None                                                                                   │ │
│ │                       backlog = 2048                                                                                   │ │
│ │                        config = <uvicorn.config.Config object at 0x76e0ce5d4e20>                                       │ │
│ │                   date_header = True                                                                                   │ │
│ │                      env_file = None                                                                                   │ │
│ │                       factory = False                                                                                  │ │
│ │                            fd = None                                                                                   │ │
│ │           forwarded_allow_ips = None                                                                                   │ │
│ │ h11_max_incomplete_event_size = None                                                                                   │ │
│ │                       headers = None                                                                                   │ │
│ │                          host = '127.0.0.1'                                                                            │ │
│ │                          http = 'auto'                                                                                 │ │
│ │                     interface = 'auto'                                                                                 │ │
│ │                      lifespan = 'auto'                                                                                 │ │
│ │             limit_concurrency = None                                                                                   │ │
│ │            limit_max_requests = None                                                                                   │ │
│ │                    log_config = {                                                                                      │ │
│ │                                 │   'version': 1,                                                                      │ │
│ │                                 │   'disable_existing_loggers': False,                                                 │ │
│ │                                 │   'formatters': {                                                                    │ │
│ │                                 │   │   'default': {                                                                   │ │
│ │                                 │   │   │   '()': <class 'fastapi_cli.utils.cli.CustomFormatter'>,                     │ │
│ │                                 │   │   │   'fmt': '%(levelprefix)s %(message)s',                                      │ │
│ │                                 │   │   │   'use_colors': None                                                         │ │
│ │                                 │   │   },                                                                             │ │
│ │                                 │   │   'access': {                                                                    │ │
│ │                                 │   │   │   '()': <class 'fastapi_cli.utils.cli.CustomFormatter'>,                     │ │
│ │                                 │   │   │   'fmt': "%(levelprefix)s %(client_addr)s - '%(request_line)s'               │ │
│ │                                 %(status_code)s"                                                                       │ │
│ │                                 │   │   }                                                                              │ │
│ │                                 │   },                                                                                 │ │
│ │                                 │   'handlers': {                                                                      │ │
│ │                                 │   │   'default': {                                                                   │ │
│ │                                 │   │   │   'formatter': 'default',                                                    │ │
│ │                                 │   │   │   'class': 'logging.StreamHandler',                                          │ │
│ │                                 │   │   │   'stream': 'ext://sys.stderr'                                               │ │
│ │                                 │   │   },                                                                             │ │
│ │                                 │   │   'access': {                                                                    │ │
│ │                                 │   │   │   'formatter': 'access',                                                     │ │
│ │                                 │   │   │   'class': 'logging.StreamHandler',                                          │ │
│ │                                 │   │   │   'stream': 'ext://sys.stdout'                                               │ │
│ │                                 │   │   }                                                                              │ │
│ │                                 │   },                                                                                 │ │
│ │                                 │   'loggers': {                                                                       │ │
│ │                                 │   │   'uvicorn': {'handlers': ['default'], 'level': 'INFO'},                         │ │
│ │                                 │   │   'uvicorn.error': {'level': 'INFO'},                                            │ │
│ │                                 │   │   'uvicorn.access': {                                                            │ │
│ │                                 │   │   │   'handlers': ['access'],                                                    │ │
│ │                                 │   │   │   'level': 'INFO',                                                           │ │
│ │                                 │   │   │   'propagate': False                                                         │ │
│ │                                 │   │   }                                                                              │ │
│ │                                 │   }                                                                                  │ │
│ │                                 }                                                                                      │ │
│ │                     log_level = None                                                                                   │ │
│ │                          loop = 'auto'                                                                                 │ │
│ │                          port = 8000                                                                                   │ │
│ │                 proxy_headers = True                                                                                   │ │
│ │                        reload = True                                                                                   │ │
│ │                  reload_delay = 0.25                                                                                   │ │
│ │                   reload_dirs = None                                                                                   │ │
│ │               reload_excludes = None                                                                                   │ │
│ │               reload_includes = None                                                                                   │ │
│ │                     root_path = ''                                                                                     │ │
│ │                        server = <uvicorn.server.Server object at 0x76e0cdb4e410>                                       │ │
│ │                 server_header = True                                                                                   │ │
│ │                          sock = <gevent._socket3.socket at 0x76e0cefc7580 object, fd=3, family=2, type=1, proto=0>     │ │
│ │                  ssl_ca_certs = None                                                                                   │ │
│ │                 ssl_cert_reqs = <VerifyMode.CERT_NONE: 0>                                                              │ │
│ │                  ssl_certfile = None                                                                                   │ │
│ │                   ssl_ciphers = 'TLSv1'                                                                                │ │
│ │                   ssl_keyfile = None                                                                                   │ │
│ │          ssl_keyfile_password = None                                                                                   │ │
│ │                   ssl_version = <_SSLMethod.PROTOCOL_TLS_SERVER: 17>                                                   │ │
│ │     timeout_graceful_shutdown = None                                                                                   │ │
│ │            timeout_keep_alive = 5                                                                                      │ │
│ │                           uds = None                                                                                   │ │
│ │                    use_colors = None                                                                                   │ │
│ │                       workers = None                                                                                   │ │
│ │                            ws = 'auto'                                                                                 │ │
│ │                  ws_max_queue = 32                                                                                     │ │
│ │                   ws_max_size = 16777216                                                                               │ │
│ │        ws_per_message_deflate = True                                                                                   │ │
│ │              ws_ping_interval = 20.0                                                                                   │ │
│ │               ws_ping_timeout = 20.0                                                                                   │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                            │
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/uvicorn/supervisors/basereload.py:52 in run              │
│                                                                                                                            │
│    49 │   │   │   self.should_exit.set()                                                                                   │
│    50 │                                                                                                                    │
│    51 │   def run(self) -> None:                                                                                           │
│ ❱  52 │   │   self.startup()                                                                                               │
│    53 │   │   for changes in self:                                                                                         │
│    54 │   │   │   if changes:                                                                                              │
│    55 │   │   │   │   logger.warning(                                                                                      │
│                                                                                                                            │
│ ╭──────────────────────────────────────── locals ─────────────────────────────────────────╮                                │
│ │ self = <uvicorn.supervisors.watchfilesreload.WatchFilesReload object at 0x76e0cdb4e7d0> │                                │
│ ╰─────────────────────────────────────────────────────────────────────────────────────────╯                                │
│                                                                                                                            │
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/uvicorn/supervisors/basereload.py:86 in startup          │
│                                                                                                                            │
│    83 │   │   │   signal.signal(sig, self.signal_handler)                                                                  │
│    84 │   │                                                                                                                │
│    85 │   │   self.process = get_subprocess(config=self.config, target=self.target, sockets=se                             │
│ ❱  86 │   │   self.process.start()                                                                                         │
│    87 │                                                                                                                    │
│    88 │   def restart(self) -> None:                                                                                       │
│    89 │   │   if sys.platform == "win32":  # pragma: py-not-win32                                                          │
│                                                                                                                            │
│ ╭───────────────────────────────────────────────────── locals ──────────────────────────────────────────────────────╮      │
│ │ color_message = 'Started reloader process [\x1b[36m\x1b[1m1006916\x1b[0m] using \x1b[36m\x1b[1mWatchFiles\x1b[0m' │      │
│ │       message = 'Started reloader process [1006916] using WatchFiles'                                             │      │
│ │          self = <uvicorn.supervisors.watchfilesreload.WatchFilesReload object at 0x76e0cdb4e7d0>                  │      │
│ │           sig = <Signals.SIGTERM: 15>                                                                             │      │
│ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯      │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/process.py:121 in start                                                                │
│                                                                                                                            │
│   118 │   │   assert not _current_process._config.get('daemon'), \                                                         │
│   119 │   │   │      'daemonic processes are not allowed to have children'                                                 │
│   120 │   │   _cleanup()                                                                                                   │
│ ❱ 121 │   │   self._popen = self._Popen(self)                                                                              │
│   122 │   │   self._sentinel = self._popen.sentinel                                                                        │
│   123 │   │   # Avoid a refcycle if the target function holds an indirect                                                  │
│   124 │   │   # reference to the process object (see bpo-30775)                                                            │
│                                                                                                                            │
│ ╭────────────────────────────── locals ──────────────────────────────╮                                                     │
│ │ self = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial> │                                                     │
│ ╰────────────────────────────────────────────────────────────────────╯                                                     │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/context.py:288 in _Popen                                                               │
│                                                                                                                            │
│   285 │   │   @staticmethod                                                                                                │
│   286 │   │   def _Popen(process_obj):                                                                                     │
│   287 │   │   │   from .popen_spawn_posix import Popen                                                                     │
│ ❱ 288 │   │   │   return Popen(process_obj)                                                                                │
│   289 │   │                                                                                                                │
│   290 │   │   @staticmethod                                                                                                │
│   291 │   │   def _after_fork():                                                                                           │
│                                                                                                                            │
│ ╭───────────────────────────────── locals ──────────────────────────────────╮                                              │
│ │ process_obj = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial> │                                              │
│ ╰───────────────────────────────────────────────────────────────────────────╯                                              │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/popen_spawn_posix.py:32 in __init__                                                    │
│                                                                                                                            │
│   29 │                                                                                                                     │
│   30 │   def __init__(self, process_obj):                                                                                  │
│   31 │   │   self._fds = []                                                                                                │
│ ❱ 32 │   │   super().__init__(process_obj)                                                                                 │
│   33 │                                                                                                                     │
│   34 │   def duplicate_for_child(self, fd):                                                                                │
│   35 │   │   self._fds.append(fd)                                                                                          │
│                                                                                                                            │
│ ╭───────────────────────────────────── locals ─────────────────────────────────────╮                                       │
│ │ process_obj = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial>        │                                       │
│ │        self = <multiprocessing.popen_spawn_posix.Popen object at 0x76e0cdb4ead0> │                                       │
│ ╰──────────────────────────────────────────────────────────────────────────────────╯                                       │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/popen_fork.py:19 in __init__                                                           │
│                                                                                                                            │
│   16 │   │   util._flush_std_streams()                                                                                     │
│   17 │   │   self.returncode = None                                                                                        │
│   18 │   │   self.finalizer = None                                                                                         │
│ ❱ 19 │   │   self._launch(process_obj)                                                                                     │
│   20 │                                                                                                                     │
│   21 │   def duplicate_for_child(self, fd):                                                                                │
│   22 │   │   return fd                                                                                                     │
│                                                                                                                            │
│ ╭───────────────────────────────────── locals ─────────────────────────────────────╮                                       │
│ │ process_obj = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial>        │                                       │
│ │        self = <multiprocessing.popen_spawn_posix.Popen object at 0x76e0cdb4ead0> │                                       │
│ ╰──────────────────────────────────────────────────────────────────────────────────╯                                       │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/popen_spawn_posix.py:47 in _launch                                                     │
│                                                                                                                            │
│   44 │   │   set_spawning_popen(self)                                                                                      │
│   45 │   │   try:                                                                                                          │
│   46 │   │   │   reduction.dump(prep_data, fp)                                                                             │
│ ❱ 47 │   │   │   reduction.dump(process_obj, fp)                                                                           │
│   48 │   │   finally:                                                                                                      │
│   49 │   │   │   set_spawning_popen(None)                                                                                  │
│   50                                                                                                                       │
│                                                                                                                            │
│ ╭──────────────────────────────────────────────────────── locals ────────────────────────────────────────────────────────╮ │
│ │               fp = <_io.BytesIO object at 0x76e0cdb36bb0>                                                              │ │
│ │        prep_data = {                                                                                                   │ │
│ │                    │   'log_to_stderr': False,                                                                         │ │
│ │                    │   'authkey':                                                                                      │ │
│ │                    b'k\xd4\xb50\xa5\xea\x16\x19\x98\xb0D^m\xd4:\x92\x19\xde&~k/Ho\x12\xaa\x8c\x83\xd3\xae\xab\x0e',    │ │
│ │                    │   'name': 'SpawnProcess-1',                                                                       │ │
│ │                    │   'sys_path': [                                                                                   │ │
│ │                    │   │   '/home/aiadmin/prop_recom_gemma',                                                           │ │
│ │                    │   │   '/home/aiadmin/prop_recom_gemma/venv2/bin',                                                 │ │
│ │                    │   │   '/usr/lib/python310.zip',                                                                   │ │
│ │                    │   │   '/usr/lib/python3.10',                                                                      │ │
│ │                    │   │   '/usr/lib/python3.10/lib-dynload',                                                          │ │
│ │                    │   │   '/home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages'                         │ │
│ │                    │   ],                                                                                              │ │
│ │                    │   'sys_argv': ['/home/aiadmin/prop_recom_gemma/venv2/bin/fastapi', 'dev', 'api.py'],              │ │
│ │                    │   'orig_dir': '/home/aiadmin/prop_recom_gemma',                                                   │ │
│ │                    │   'dir': '/home/aiadmin/prop_recom_gemma',                                                        │ │
│ │                    │   'start_method': 'fork',                                                                         │ │
│ │                    │   'init_main_from_path': '/home/aiadmin/prop_recom_gemma/venv2/bin/fastapi'                       │ │
│ │                    }                                                                                                   │ │
│ │      process_obj = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial>                                         │ │
│ │ resource_tracker = <module 'multiprocessing.resource_tracker' from                                                     │ │
│ │                    '/usr/lib/python3.10/multiprocessing/resource_tracker.py'>                                          │ │
│ │             self = <multiprocessing.popen_spawn_posix.Popen object at 0x76e0cdb4ead0>                                  │ │
│ │       tracker_fd = 7                                                                                                   │ │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                            │
│ /usr/lib/python3.10/multiprocessing/reduction.py:60 in dump                                                                │
│                                                                                                                            │
│    57                                                                                                                      │
│    58 def dump(obj, file, protocol=None):                                                                                  │
│    59 │   '''Replacement for pickle.dump() using ForkingPickler.'''                                                        │
│ ❱  60 │   ForkingPickler(file, protocol).dump(obj)                                                                         │
│    61                                                                                                                      │
│    62 #                                                                                                                    │
│    63 # Platform specific definitions                                                                                      │
│                                                                                                                            │
│ ╭──────────────────────────────── locals ────────────────────────────────╮                                                 │
│ │     file = <_io.BytesIO object at 0x76e0cdb36bb0>                      │                                                 │
│ │      obj = <SpawnProcess name='SpawnProcess-1' parent=1006916 initial> │                                                 │
│ │ protocol = None                                                        │                                                 │
│ ╰────────────────────────────────────────────────────────────────────────╯                                                 │
│                                                                                                                            │
│ /home/aiadmin/prop_recom_gemma/venv2/lib/python3.10/site-packages/gevent/_socket3.py:186 in __getstate__                   │
│                                                                                                                            │
│   183 │   │   return ''                                                                                                    │
│   184 │                                                                                                                    │
│   185 │   def __getstate__(self):                                                                                          │
│ ❱ 186 │   │   raise TypeError("Cannot serialize socket object")                                                            │
│   187 │                                                                                                                    │
│   188 │   def dup(self):                                                                                                   │
│   189 │   │   """dup() -> socket object                                                                                    │
│                                                                                                                            │
│ ╭───────────────────────────────────────── locals ──────────────────────────────────────────╮                              │
│ │ self = <gevent._socket3.socket at 0x76e0cefc7580 object, fd=3, family=2, type=1, proto=0> │                              │
│ ╰───────────────────────────────────────────────────────────────────────────────────────────╯                              │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: Cannot serialize socket object
@spyoungtech
Copy link
Owner

Because this library depends on gevent and its monkey-patching, it can sometimes cause unexpected conflicts when used in combination with other libraries. In some cases, these compatibility issues are not easily reconcilable, if at all.

As a stab in the dark, you can try running your server with gunicorn which should use a pre-fork server instead of using spawn which may avoid the serialization issue. Not sure.

That said, if you're using async and FastAPI, you would probably best be served by using an asyncio-compatible HTTP client library.

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

2 participants