Skip to content

Commit d6a6eab

Browse files
committed
ignore closed event loop error in async trie shutdown
1 parent c4b94a5 commit d6a6eab

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

genlm_backend/trie/async_impl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ async def cleanup(self):
146146
def shutdown(self):
147147
"""Stop the background processing task and cleanup resources."""
148148
if self._task is not None:
149-
self._task.cancel()
149+
try:
150+
self._task.cancel()
151+
except RuntimeError:
152+
# Ignore runtime errors that might occur if event loop is closed
153+
pass
150154
self._task = None
151155

152156
def __del__(self):

tests/test_trie.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,15 @@ async def test_async_trie(mock_llm, backend):
173173
for have, want in zip(haves, wants):
174174
np.testing.assert_allclose(have, want, rtol=1e-5, atol=1e-8)
175175

176-
# test cleanup
176+
177+
@pytest.mark.asyncio
178+
@pytest.mark.parametrize("backend", ["sequential", "parallel"])
179+
async def test_async_trie_cleanup(mock_llm, backend):
180+
async_trie = AsyncTokenCharacterTrie.from_vocab(
181+
mock_llm.byte_vocab, backend=backend
182+
)
177183
await async_trie.cleanup()
184+
assert async_trie._task is None
178185

179186

180187
def test_sequential_preprocessing(decode):

0 commit comments

Comments
 (0)