Skip to content

Commit c168e9b

Browse files
Copilotbashandbone
andauthored
fix: use time.monotonic() in ResourceGovernor to prevent Python 3.12 CI hang (#336)
* fix: use time.monotonic() in ResourceGovernor and remove harmful time.time mocks from fixtures Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> * Fix condition for elapsed_seconds calculation Signed-off-by: Adam Poulemanos <89049923+bashandbone@users.noreply.github.com> --------- Signed-off-by: Adam Poulemanos <89049923+bashandbone@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
1 parent 90d81cf commit c168e9b

3 files changed

Lines changed: 15 additions & 38 deletions

File tree

src/codeweaver/engine/chunker/governance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __enter__(self) -> Self:
8383
Returns:
8484
Self reference for use in with statement
8585
"""
86-
self._start_time = time.time()
86+
self._start_time = time.monotonic()
8787
self._chunk_count = 0
8888
return self
8989

@@ -115,7 +115,7 @@ def check_timeout(self) -> None:
115115
if self._start_time is None:
116116
return
117117

118-
elapsed = time.time() - self._start_time
118+
elapsed = time.monotonic() - self._start_time
119119
if elapsed > self.settings.chunk_timeout_seconds:
120120
# Log resource limit violation for observability
121121
from pathlib import Path
@@ -157,7 +157,7 @@ def check_chunk_limit(self) -> None:
157157
limit_value=float(self.settings.max_chunks_per_file),
158158
actual_value=float(self._chunk_count),
159159
extra_context={
160-
"elapsed_seconds": time.time() - self._start_time if self._start_time else 0
160+
"elapsed_seconds": time.monotonic() - self._start_time if self._start_time is not None else 0
161161
},
162162
)
163163

tests/integration/conftest.py

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from collections.abc import AsyncGenerator, Generator
1616
from pathlib import Path
1717
from typing import TYPE_CHECKING, Any
18-
from unittest.mock import AsyncMock, MagicMock, patch
18+
from unittest.mock import AsyncMock, MagicMock
1919

2020
import pytest
2121

@@ -534,16 +534,8 @@ def configured_providers(
534534
container.override(VectorStoreProvider, mock_vector_store)
535535
container.override(RerankingProvider, mock_reranking_provider)
536536

537-
call_count = [0] # Use list for mutable counter
538-
539-
def mock_time() -> float:
540-
call_count[0] += 1
541-
# Return monotonically increasing time values (start from a baseline)
542-
return 1000000.0 + call_count[0] * 0.001
543-
544-
with patch("time.time", side_effect=mock_time):
545-
yield
546-
container.clear_overrides()
537+
yield
538+
container.clear_overrides()
547539

548540

549541
# ===========================================================================
@@ -1025,15 +1017,8 @@ def real_providers(
10251017
container.override(VectorStoreProvider, real_vector_store)
10261018
container.override(RerankingProvider, real_reranking_provider)
10271019

1028-
call_count = [0]
1029-
1030-
def mock_time() -> float:
1031-
call_count[0] += 1
1032-
return 1000000.0 + call_count[0] * 0.001
1033-
1034-
with patch("time.time", side_effect=mock_time):
1035-
yield
1036-
container.clear_overrides()
1020+
yield
1021+
container.clear_overrides()
10371022

10381023

10391024
# ===========================================================================
@@ -1221,23 +1206,15 @@ async def get_test_settings() -> CodeWeaverSettingsType:
12211206

12221207
server._state = state
12231208

1224-
# Patch time for deterministic behavior if needed
1225-
call_count = [0]
1226-
1227-
def mock_time() -> float:
1228-
call_count[0] += 1
1229-
return 1000000.0 + call_count[0] * 0.001
1230-
1231-
with patch("time.time", side_effect=mock_time):
1232-
# Resolve indexer from container
1233-
indexer = await clean_container.resolve(IndexingService)
1209+
# Resolve indexer from container
1210+
indexer = await clean_container.resolve(IndexingService)
12341211

1235-
# Ensure it's using the correct project path
1236-
indexer._project_path = project_path
1212+
# Ensure it's using the correct project path
1213+
indexer._project_path = project_path
12371214

1238-
await indexer.index_project(force_reindex=True)
1215+
await indexer.index_project(force_reindex=True)
12391216

1240-
yield project_path
1217+
yield project_path
12411218

12421219
# Cleanup: Reset global state
12431220
from codeweaver.server import server

tests/unit/engine/chunker/test_governance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_timeout_enforcement():
4242

4343
with patch("codeweaver.engine.chunker.governance.time") as mock_time:
4444
# Set up time progression: 0.0 at __enter__, 1.5 at check_timeout()
45-
mock_time.time.side_effect = [0.0, 1.5]
45+
mock_time.monotonic.side_effect = [0.0, 1.5]
4646

4747
with pytest.raises(ChunkingTimeoutError, match="exceeded timeout"):
4848
with ResourceGovernor(settings) as governor:

0 commit comments

Comments
 (0)