Skip to content

Commit 61f95b3

Browse files
committed
test that nest_asycnio does not block
1 parent e166164 commit 61f95b3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Diff for: pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ dev-dependencies = [
6565
"azure-identity >=1.14.1",
6666
"types-tqdm > 4",
6767
"types-pyaudio > 0",
68-
"trio >=0.22.2"
68+
"trio >=0.22.2",
69+
"nest_asyncio==1.6.0"
70+
6971
]
7072

7173
[tool.rye.scripts]

Diff for: tests/test_client.py

+43
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
import gc
66
import os
7+
import sys
78
import json
89
import asyncio
910
import inspect
11+
import subprocess
1012
import tracemalloc
1113
from typing import Any, Union, cast
14+
from textwrap import dedent
1215
from unittest import mock
1316
from typing_extensions import Literal
1417

@@ -1766,3 +1769,43 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
17661769
) as response:
17671770
assert response.retries_taken == failures_before_success
17681771
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
1772+
1773+
def test_get_platform(self) -> None:
1774+
# Issue https://github.com/openai/openai-python/issues/1827 was caused
1775+
# asyncify leaving threads unterminated when used with nest_asyncio.
1776+
# Since nest_asyncio.apply() is global and cannot be un-applied, this
1777+
# test is run in a separate process to avoid affecting other tests.
1778+
test_code = dedent("""\
1779+
import asyncio
1780+
import nest_asyncio
1781+
1782+
import threading
1783+
1784+
from openai._base_client import get_platform
1785+
from openai._utils import asyncify
1786+
1787+
async def test_main() -> None:
1788+
result = await asyncify(get_platform)()
1789+
print(result)
1790+
for thread in threading.enumerate():
1791+
print(thread.name)
1792+
1793+
nest_asyncio.apply()
1794+
asyncio.run(test_main())
1795+
""")
1796+
with subprocess.Popen(
1797+
[sys.executable, "-c", test_code],
1798+
stdout=subprocess.PIPE,
1799+
stderr=subprocess.PIPE,
1800+
text=True,
1801+
) as process:
1802+
try:
1803+
process.wait(2)
1804+
if process.returncode:
1805+
print(process.stdout)
1806+
print(process.stderr)
1807+
raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code")
1808+
except subprocess.TimeoutExpired as e:
1809+
process.kill()
1810+
raise AssertionError("calling get_platform using asyncify resulted in a hung process") from e
1811+

0 commit comments

Comments
 (0)