mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-05 14:19:56 -05:00
videos: channel POST begins as a writer
POST /api/videos/channels failed with "database is locked" on
`INSERT INTO collection` on the docker WROLPi while background workers
(config saves, refreshes) were writing. The handler wrote through the
deferred request session: `create_channel` reads (the URL/name/directory
conflict check) and then INSERTs, and SQLite refuses that mid-transaction
lock upgrade instantly when another connection wrote in between --
busy_timeout is skipped for upgrades.
Same shape and same fix as POST /api/download (daf2ba39): the handler
now uses `get_db_session(commit=True)`, which begins IMMEDIATE so the
concurrent writer waits its turn, and which commits before the refresh
is fired so the refresh does not wait on a lock the request still holds.
The regression test commits a write from a second connection between
the handler's conflict check and its INSERT. The existing
`write_lock_held_briefly` helper cannot reproduce this: the Sanic test
client takes over a second to start the app, so its lock is released
before the handler runs. Before the fix the test fails with the
production error verbatim.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
c2ffc9286e
commit
f61cbb6e81
2 changed files with 66 additions and 5 deletions
|
|
@ -9,6 +9,7 @@ from sanic_ext.extensions.openapi import openapi
|
|||
|
||||
from wrolpi.api_utils import json_response
|
||||
from wrolpi.common import logger, wrol_mode_check, get_media_directory, get_relative_to_media_directory
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.events import Events
|
||||
from wrolpi.schema import JSONErrorResponse
|
||||
from wrolpi.vars import PYTEST
|
||||
|
|
@ -51,18 +52,24 @@ def channel_get(request: Request, channel_id: int = None):
|
|||
@validate(schema.ChannelPostRequest)
|
||||
@wrol_mode_check
|
||||
def channel_post(request: Request, body: schema.ChannelPostRequest):
|
||||
session = request.ctx.session
|
||||
body.directory = get_media_directory() / body.directory
|
||||
|
||||
channel = lib.create_channel(session, data=body, return_dict=False)
|
||||
# Not `request.ctx.session`: creating a Channel reads (the URL/name/directory conflict check)
|
||||
# before it INSERTs, and a deferred transaction fails that lock upgrade *instantly* with
|
||||
# "database is locked" if anything else wrote in between -- which is what a user creating a
|
||||
# Channel while background workers saved configs hit. This session also commits before the
|
||||
# refresh below is fired, so the refresh does not wait on a lock this request still holds.
|
||||
with get_db_session(commit=True) as session:
|
||||
channel = lib.create_channel(session, data=body, return_dict=False)
|
||||
channel_id, channel_name = channel.id, channel.name
|
||||
|
||||
# Refresh the videos asynchronously
|
||||
channel.refresh_files(channel.id)
|
||||
Channel.refresh_files(channel_id)
|
||||
|
||||
Events.send_created(f'Created Channel: {channel.name}')
|
||||
Events.send_created(f'Created Channel: {channel_name}')
|
||||
|
||||
return response.json({'success': 'Channel created successfully'}, HTTPStatus.CREATED,
|
||||
{'Location': f'/api/videos/channels/{channel.id}'})
|
||||
{'Location': f'/api/videos/channels/{channel_id}'})
|
||||
|
||||
|
||||
@channel_bp.put('/<channel_id:int>')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue