Renaming a Tag commits its Download renames with the request

update_tag renamed Downloads in a background task using the request's
session, after RequestDB.write() had committed.  Its flush was never
committed, so the rename was lost, and the open write transaction held
SQLite's write lock: every later write failed with "database is locked"
until the API restarted.  Only the Channel moves stay in the background.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-25 16:56:51 -06:00
parent c577cae9d8
commit ac2906bff9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 40 additions and 27 deletions

View file

@ -182,22 +182,23 @@ class Tag(ModelHelper, Base):
if Session.object_session(self):
self.flush()
# Rename old name referenced in any Downloads, in the caller's transaction. (A background task's writes
# would not be committed with it.)
from wrolpi.downloader import Download
to_flush = list()
for download in session.query(Download):
tag_names, settings = with_tag_name_renamed(download.tag_names, download.settings, old_name, name)
if tag_names != download.tag_names or settings != download.settings:
download.tag_names, download.settings = tag_names, settings
to_flush.append(download)
if to_flush:
session.flush(to_flush)
save_downloads_config.activate_switch()
from modules.videos.errors import ChannelDirectoryConflict
async def _():
# Rename old name referenced in any Downloads.
from wrolpi.downloader import Download
to_flush = list()
for download in session.query(Download):
tag_names, settings = with_tag_name_renamed(download.tag_names, download.settings, old_name, name)
if tag_names != download.tag_names or settings != download.settings:
download.tag_names, download.settings = tag_names, settings
to_flush.append(download)
if to_flush:
session.flush(to_flush)
save_downloads_config.activate_switch()
# Only move channels if tag is being renamed (not created)
if old_name:
# Get channels through Collections

View file

@ -596,27 +596,39 @@ async def test_tag_rename_with_channel(test_session, test_directory, video_facto
@pytest.mark.asyncio
async def test_tag_rename_downloads(test_session, tag_factory, await_background_tasks):
async def test_tag_rename_downloads(async_client, test_session, tag_factory, await_background_tasks):
"""Renaming a Tag renames it everywhere a Download applies it, including the settings passed on to the
Downloads (and Channels) it creates."""
Downloads (and Channels) it creates. The rename is committed with the request, and leaves the database
writable."""
from wrolpi.db import get_db_session
from wrolpi.downloader import Download
tag, other = await tag_factory('one'), await tag_factory('two')
download = Download(url='https://example.com/playlist', downloader='video_channel', tag_names=['one', 'two'],
settings=dict(tag_names=['two', 'one'], channel_tag_name=['one'], video_resolutions=['720p']))
untouched = Download(url='https://example.com/other', downloader='video_channel', tag_names=['two'],
settings=dict(tag_names=['two'], channel_tag_name=['two']))
test_session.add_all([download, untouched])
test_session.add_all([
Download(url='https://example.com/playlist', downloader='video_channel', tag_names=['one', 'two'],
settings=dict(tag_names=['two', 'one'], channel_tag_name=['one'], video_resolutions=['720p'])),
Download(url='https://example.com/other', downloader='video_channel', tag_names=['two'],
settings=dict(tag_names=['two'], channel_tag_name=['two'])),
])
test_session.commit()
await tag.update_tag(test_session, 'New Name', None)
content = dict(name='New Name', color=tag.color)
request, response = await async_client.post(f'/api/tag/{tag.id}', content=json.dumps(content))
assert response.status_code == HTTPStatus.OK
await await_background_tasks()
test_session.commit()
assert download.tag_names == ['New Name', 'two']
assert download.settings == dict(tag_names=['two', 'New Name'], channel_tag_name=['New Name'],
video_resolutions=['720p'])
assert untouched.tag_names == ['two']
assert untouched.settings == dict(tag_names=['two'], channel_tag_name=['two'])
# Read what was committed, not what a session holds.
with get_db_session() as session:
downloads = {i.url: (i.tag_names, i.settings) for i in session.query(Download)}
assert downloads == {
'https://example.com/playlist': (['New Name', 'two'], dict(tag_names=['two', 'New Name'],
channel_tag_name=['New Name'],
video_resolutions=['720p'])),
'https://example.com/other': (['two'], dict(tag_names=['two'], channel_tag_name=['two'])),
}
# Nothing holds the write lock.
request, response = await async_client.post('/api/tag', content=json.dumps(dict(name='three', color='#000000')))
assert response.status_code == HTTPStatus.CREATED
@pytest.mark.asyncio