From 166d6a47c3579d60602c105e246a82e312cbd9a7 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 24 Dec 2025 17:42:17 -0700 Subject: [PATCH] Removing PYTEST from channel move to avoid missing session. --- modules/videos/channel/lib.py | 40 ++++++++++++++------- modules/videos/channel/test/test_api.py | 5 ++- modules/videos/channel/test/test_lib.py | 48 +++++++++++++++++++++++++ wrolpi/conftest.py | 15 ++++++-- 4 files changed, 91 insertions(+), 17 deletions(-) diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 53a35bae..9def1e7e 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -12,8 +12,8 @@ from wrolpi.common import logger, \ from wrolpi.db import get_db_curs, get_db_session from wrolpi.downloader import save_downloads_config, download_manager, Download from wrolpi.errors import APIError, ValidationError, RefreshConflict +from wrolpi.events import Events from wrolpi.tags import Tag -from wrolpi.vars import PYTEST from .. import schema from ..common import check_for_channel_conflicts from ..errors import UnknownChannel @@ -113,17 +113,13 @@ async def update_channel(session: Session, *, data: schema.ChannelPutRequest, ch session.commit() - async def _(): - await channel.move_channel(new_directory, session, send_events=True) - if new_directory != old_directory: if not new_directory.is_dir(): new_directory.mkdir(parents=True) - if PYTEST: - await _() - else: - background_task(_()) + # Always use background task with its own session to avoid + # DetachedInstanceError when the original session is closed. + background_task(_background_move_channel(channel.id, new_directory)) save_channels_config.activate_switch() @@ -247,6 +243,26 @@ async def update_channel_download(channel_id: int, download_id: int, url: str, f return download +async def _background_move_channel(channel_id: int, target_directory: pathlib.Path): + """Helper to run channel move in background with its own database session. + + This allows the move to continue even if the original HTTP request is cancelled + (e.g., user closes browser tab), and avoids DetachedInstanceError by creating + a fresh session for the background task. + """ + try: + with get_db_session(commit=True) as session: + channel = Channel.find_by_id(session, channel_id) + if not channel: + logger.error(f'_background_move_channel: channel {channel_id} not found') + Events.send_file_move_failed(f'Channel move failed: channel not found') + return + await channel.move_channel(target_directory, session, send_events=True) + except Exception as e: + logger.error(f'_background_move_channel: failed for channel_id={channel_id}', exc_info=e) + Events.send_file_move_failed(f'Channel move failed: {e}') + + @wrol_mode_check async def tag_channel(session: Session, tag_name: str | None, directory: pathlib.Path | None, channel_id: int): """Add a Tag to a Channel, or remove a Tag from a Channel if no `tag_name` is provided. @@ -268,11 +284,9 @@ async def tag_channel(session: Session, tag_name: str | None, directory: pathlib # Move to newly defined directory only if necessary. directory.mkdir(parents=True, exist_ok=True) if channel.directory != directory: - coro = channel.move_channel(directory, session, send_events=True) - if PYTEST: - await coro - else: - background_task(coro) + # Always use background task with its own session to avoid + # DetachedInstanceError when the original session is closed. + background_task(_background_move_channel(channel.id, directory)) else: save_channels_config.activate_switch() else: diff --git a/modules/videos/channel/test/test_api.py b/modules/videos/channel/test/test_api.py index 87ffa550..2fb944ec 100644 --- a/modules/videos/channel/test/test_api.py +++ b/modules/videos/channel/test/test_api.py @@ -467,7 +467,7 @@ async def test_channel_download_id(async_client, test_session, tag_factory, simp @pytest.mark.asyncio async def test_tag_channel(async_client, test_session, test_directory, channel_factory, tag_factory, video_factory, - test_channels_config, test_download_manager, test_downloader): + test_channels_config, test_download_manager, test_downloader, await_background_tasks): """A single Tag can be applied to a Channel.""" # Create channel directory in the usual videos directory. videos_directory = get_videos_directory() @@ -498,6 +498,7 @@ async def test_tag_channel(async_client, test_session, test_directory, channel_f body = dict(tag_name=tag.name, directory='videos/Tag Name/Channel Name') request, response = await async_client.post(f'/api/videos/channels/{channel.id}/tag', json=body) assert response.status_code == HTTPStatus.NO_CONTENT + await await_background_tasks() # Wait for move to complete and expire session objects channel = test_session.query(Channel).one() assert channel.tag_name == tag.name @@ -529,6 +530,8 @@ async def test_tag_channel(async_client, test_session, test_directory, channel_f body = dict(tag_name=None, directory='videos/Channel Name') request, response = await async_client.post(f'/api/videos/channels/{channel.id}/tag', json=body) assert response.status_code == HTTPStatus.NO_CONTENT, response.content.decode() + await await_background_tasks() # Wait for move to complete and expire session objects + channel = test_session.query(Channel).one() assert channel.tag_name is None assert str(channel.directory) == str(test_directory / 'videos/Channel Name') diff --git a/modules/videos/channel/test/test_lib.py b/modules/videos/channel/test/test_lib.py index fb2ab490..2cabe1a8 100644 --- a/modules/videos/channel/test/test_lib.py +++ b/modules/videos/channel/test/test_lib.py @@ -199,3 +199,51 @@ async def test_untag_channel_without_directory_saves_config( channel_entry = next((c for c in config.get('channels', []) if c['name'] == 'Channel Name'), None) assert channel_entry is not None, "Channel not found in config" assert channel_entry.get('tag_name') is None, "Tag should be removed from config" + + +@pytest.mark.asyncio +async def test_tag_channel_move_background_task( + test_session, async_client, test_directory, channel_factory, tag_factory, + await_background_tasks, test_channels_config +): + """Test that channel move works when executed as a background task. + + This replicates the production scenario where PYTEST=False causes + move_channel to run via background_task() instead of direct await. + + Bug: When PYTEST=False, tag_channel passes a session to background_task(), + but by the time the task runs, the session is invalid, causing + DetachedInstanceError when accessing channel.collection. + """ + from unittest import mock + + # Create channel with files + original_dir = test_directory / 'videos' / 'test_channel' + original_dir.mkdir(parents=True) + channel = channel_factory(name='Test Channel', directory=original_dir) + test_session.commit() + + # Create a test file in the channel directory + test_file = original_dir / 'test_video.mp4' + test_file.touch() + + new_dir = test_directory / 'videos' / 'moved_channel' + new_dir.mkdir(parents=True, exist_ok=True) + + tag = await tag_factory(name='MovedTag') + channel_id = channel.id + + # Call tag_channel - it will use background_task() for the move. + # The fix ensures the background task creates its own session, so + # this should work regardless of whether the original session is closed. + await lib.tag_channel(test_session, tag.name, new_dir, channel_id) + + # Wait for background task to complete and expire session objects + await await_background_tasks() + + # Verify move succeeded - refresh channel from DB + channel = Channel.find_by_id(test_session, channel_id) + assert channel is not None, "Channel should still exist" + assert channel.directory == new_dir, f"Channel directory should be {new_dir}, got {channel.directory}" + assert (new_dir / 'test_video.mp4').exists(), "File should have been moved to new directory" + assert not test_file.exists(), "File should no longer exist in original directory" diff --git a/wrolpi/conftest.py b/wrolpi/conftest.py index 2538395c..159a720e 100644 --- a/wrolpi/conftest.py +++ b/wrolpi/conftest.py @@ -178,10 +178,19 @@ async def await_switches(async_client): @pytest.fixture -async def await_background_tasks(async_client): - """Returns the `switches.await_switches` function, but primes the Sanic App to actually handle signals.""" +async def await_background_tasks(async_client, test_session): + """Returns a function that awaits background tasks and expires session objects. + + After background tasks complete, session objects may be stale, so we automatically + expire them to force a refresh on next access. + """ await async_client.get('/api') - return await_background_tasks_ + + async def _await_and_expire(): + await await_background_tasks_() + test_session.expire_all() + + return _await_and_expire @pytest.fixture