|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import logging |
| 16 | +import os |
16 | 17 | import sys |
17 | 18 | import tempfile |
18 | 19 | import types |
19 | 20 | from datetime import datetime, timedelta, timezone |
20 | | -from unittest.mock import MagicMock |
| 21 | +from unittest.mock import MagicMock, patch |
21 | 22 |
|
22 | 23 | import dill |
23 | 24 | import pytest |
| 25 | +from sqlalchemy.pool import QueuePool |
24 | 26 |
|
25 | 27 | from feast import Field |
26 | 28 | from feast.data_source import PushSource |
@@ -331,6 +333,60 @@ def test_warn_if_narrow_blob_columns_swallows_query_errors(caplog): |
331 | 333 | assert "still typed BLOB" not in caplog.text |
332 | 334 |
|
333 | 335 |
|
| 336 | +@pytest.mark.skipif(not hasattr(os, "fork"), reason="requires os.fork") |
| 337 | +@pytest.mark.filterwarnings( |
| 338 | + "ignore:This process .* is multi-threaded:DeprecationWarning" |
| 339 | +) |
| 340 | +def test_forked_child_does_not_reuse_parent_pooled_connection(sqlite_registry): |
| 341 | + """A forked child (e.g. a Gunicorn worker) must not share the parent's pooled connections.""" |
| 342 | + assert isinstance(sqlite_registry.write_engine.pool, QueuePool) |
| 343 | + with sqlite_registry.write_engine.connect() as conn: |
| 344 | + parent_conn = conn.connection.dbapi_connection |
| 345 | + |
| 346 | + pid = os.fork() |
| 347 | + if pid == 0: |
| 348 | + try: |
| 349 | + sqlite_registry.reset_after_fork() |
| 350 | + with sqlite_registry.write_engine.connect() as conn: |
| 351 | + child_conn = conn.connection.dbapi_connection |
| 352 | + os._exit(1 if child_conn is parent_conn else 0) |
| 353 | + except BaseException: |
| 354 | + os._exit(2) |
| 355 | + _, status = os.waitpid(pid, 0) |
| 356 | + |
| 357 | + assert os.waitstatus_to_exitcode(status) == 0 |
| 358 | + # The parent's pooled connection is still usable. |
| 359 | + assert parent_conn.execute("SELECT 1").fetchone() == (1,) |
| 360 | + |
| 361 | + |
| 362 | +def test_reset_after_fork_disposes_pool_without_closing(sqlite_registry): |
| 363 | + with patch.object(sqlite_registry.write_engine, "dispose") as dispose: |
| 364 | + sqlite_registry.reset_after_fork() |
| 365 | + |
| 366 | + dispose.assert_called_once_with(close=False) |
| 367 | + |
| 368 | + |
| 369 | +def test_reset_after_fork_disposes_read_and_write_engines(tmp_path): |
| 370 | + config = SqlRegistryConfig( |
| 371 | + registry_type="sql", |
| 372 | + path=f"sqlite:///{tmp_path / 'registry.db'}", |
| 373 | + read_path=f"sqlite:///{tmp_path / 'registry.db'}", |
| 374 | + purge_feast_metadata=False, |
| 375 | + ) |
| 376 | + registry = SqlRegistry(config, "test_project", None) |
| 377 | + try: |
| 378 | + with ( |
| 379 | + patch.object(registry.write_engine, "dispose") as write_dispose, |
| 380 | + patch.object(registry.read_engine, "dispose") as read_dispose, |
| 381 | + ): |
| 382 | + registry.reset_after_fork() |
| 383 | + |
| 384 | + write_dispose.assert_called_once_with(close=False) |
| 385 | + read_dispose.assert_called_once_with(close=False) |
| 386 | + finally: |
| 387 | + registry.teardown() |
| 388 | + |
| 389 | + |
334 | 390 | def test_sql_registry(sqlite_registry): |
335 | 391 | """ |
336 | 392 | Test the SQL registry |
|
0 commit comments