Skip to content

Commit

Permalink
fix: prevent crash when platform.architecture() is not allowed (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Feb 4, 2024
1 parent 66bab65 commit 2384e27
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,12 @@ def __str__(self) -> str:


def get_platform() -> Platform:
system = platform.system().lower()
platform_name = platform.platform().lower()
try:
system = platform.system().lower()
platform_name = platform.platform().lower()
except Exception:
return "Unknown"

if "iphone" in platform_name or "ipad" in platform_name:
# Tested using Python3IDE on an iPhone 11 and Pythonista on an iPad 7
# system is Darwin and platform_name is a string like:
Expand Down Expand Up @@ -1880,8 +1884,8 @@ def platform_headers(version: str) -> Dict[str, str]:
"X-Stainless-Package-Version": version,
"X-Stainless-OS": str(get_platform()),
"X-Stainless-Arch": str(get_architecture()),
"X-Stainless-Runtime": platform.python_implementation(),
"X-Stainless-Runtime-Version": platform.python_version(),
"X-Stainless-Runtime": get_python_runtime(),
"X-Stainless-Runtime-Version": get_python_version(),
}


Expand All @@ -1897,9 +1901,27 @@ def __str__(self) -> str:
Arch = Union[OtherArch, Literal["x32", "x64", "arm", "arm64", "unknown"]]


def get_python_runtime() -> str:
try:
return platform.python_implementation()
except Exception:
return "unknown"


def get_python_version() -> str:
try:
return platform.python_version()
except Exception:
return "unknown"


def get_architecture() -> Arch:
python_bitness, _ = platform.architecture()
machine = platform.machine().lower()
try:
python_bitness, _ = platform.architecture()
machine = platform.machine().lower()
except Exception:
return "unknown"

if machine in ("arm64", "aarch64"):
return "arm64"

Expand Down

2 comments on commit 2384e27

@robcalvillo
Copy link

@robcalvillo robcalvillo commented on 2384e27 Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stainless-bot . I'm finding the fix in L1919 insufficient. My setup with Celery and OpenAI works OK. However, when I add NLTK to the mix, for some reason, the app crashes precisely on that line. The try block doesn't seem to catch the exception that unfolds:

[2024-07-18 19:02:07,828: ERROR/MainProcess] Process 'ForkPoolWorker-4' pid:30848 exited with 'signal 4 (SIGILL)'
[2024-07-18 19:02:07,848: ERROR/MainProcess] Task handler raised error: WorkerLostError('Worker exited prematurely: signal 4 (SIGILL) Job: 3.')
Traceback (most recent call last):
  File "/.venv/lib/python3.11/site-packages/billiard/pool.py", line 1264, in mark_as_worker_lost
    raise WorkerLostError(
billiard.einfo.ExceptionWithTraceback: 
"""
Traceback (most recent call last):
  File "/.venv/lib/python3.11/site-packages/billiard/pool.py", line 1264, in mark_as_worker_lost
    raise WorkerLostError(
billiard.exceptions.WorkerLostError: Worker exited prematurely: signal 4 (SIGILL) Job: 3.
"""

@issamhaimour
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stainless-bot . I'm finding the fix in L1919 insufficient. My setup with Celery and OpenAI works OK. However, when I add NLTK to the mix, for some reason, the app crashes precisely on that line. The try block doesn't seem to catch the exception that unfolds:

[2024-07-18 19:02:07,828: ERROR/MainProcess] Process 'ForkPoolWorker-4' pid:30848 exited with 'signal 4 (SIGILL)'
[2024-07-18 19:02:07,848: ERROR/MainProcess] Task handler raised error: WorkerLostError('Worker exited prematurely: signal 4 (SIGILL) Job: 3.')
Traceback (most recent call last):
  File "/.venv/lib/python3.11/site-packages/billiard/pool.py", line 1264, in mark_as_worker_lost
    raise WorkerLostError(
billiard.einfo.ExceptionWithTraceback: 
"""
Traceback (most recent call last):
  File "/.venv/lib/python3.11/site-packages/billiard/pool.py", line 1264, in mark_as_worker_lost
    raise WorkerLostError(
billiard.exceptions.WorkerLostError: Worker exited prematurely: signal 4 (SIGILL) Job: 3.
"""

Please sign in to comment.