mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-28 19:06:19 -05:00
Tests fail when a request writes outside request.ctx.db.write() (#864)
Some checks are pending
Backend Tests / api-tests (core) (push) Waiting to run
Backend Tests / api-tests (files) (push) Waiting to run
Backend Tests / api-tests (media) (push) Waiting to run
Backend Tests / api-tests (rest) (push) Waiting to run
Backend Tests / api-contract-tests (push) Waiting to run
Backend Tests / api-migrations-and-start (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Cypress Tests / cypress-component-tests (push) Waiting to run
Cypress Tests / cypress-e2e-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run
Some checks are pending
Backend Tests / api-tests (core) (push) Waiting to run
Backend Tests / api-tests (files) (push) Waiting to run
Backend Tests / api-tests (media) (push) Waiting to run
Backend Tests / api-tests (rest) (push) Waiting to run
Backend Tests / api-contract-tests (push) Waiting to run
Backend Tests / api-migrations-and-start (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Cypress Tests / cypress-component-tests (push) Waiting to run
Cypress Tests / cypress-e2e-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run
* Tests fail when a request writes outside request.ctx.db.write()
A request's transaction begins deferred. A write in it after a read fails
instantly ("database is locked") when another connection wrote in between,
and a write left for the response middleware's commit can be lost after the
response was sent. #846-#853 and #863 moved handlers' writes into
request.ctx.db.write(); this keeps it that way.
Under pytest, a listener records every INSERT/UPDATE/DELETE/REPLACE made
while a request runs (or in a task it started) outside RequestDB.write()
or get_db_session(commit=True), and the autouse no_deferred_request_writes
fixture fails the test. allow_deferred_request_writes opts a test out.
The test-only response listener now runs switches outside the request,
as production's perpetual process does.
It found three more:
- DELETE /api/zim/<ids> deleted the .zim file, returned 204, then lost the
row's delete when the middleware's commit failed under contention. It
now writes in request.ctx.db.write().
- Viewing a file recorded the view in a background task on the request's
Session, after the request ended. set_file_viewed opens its own write.
- tags.import_config (startup and config import) read, then wrote, in a
deferred get_db_session(). It now begins as a writer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Tasks don't inherit a declared write; tags import change backed out
From review of #864:
- background_task's task copied its creator's Context, including
_write_intent when started inside get_db_session(commit=True) or
RequestDB.write(), so the guard ignored the task's own writes for its
lifetime. The task now starts with no declared write. That exposes
fetch_channel_source_id (started by the channels config import): it read
the Channel, asked the remote site for its source_id, then committed in
the same deferred transaction. It now reads, fetches with no
transaction open, then writes in a short get_db_session(commit=True).
- tags.import_config: commit=True held the write lock through its directory
walk, and its exit commit saved changes the need_commit gate used to
drop. Reverted to master; splitting its reads from a short write is its
own change. The two tests that run it through a request are marked
allow_deferred_request_writes until then.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
604d467861
commit
a93630576f
13 changed files with 179 additions and 23 deletions
|
|
@ -909,15 +909,20 @@ async def fetch_channel_source_id(channel_id: int):
|
|||
# If we can download from a channel, we must have its source_id.
|
||||
try:
|
||||
with get_db_session() as session:
|
||||
from modules.videos.channel.lib import get_channel
|
||||
channel = Channel.find_by_id(session, channel_id)
|
||||
source_id, url = channel.source_id, channel.url
|
||||
if source_id:
|
||||
return
|
||||
|
||||
channel.source_id = channel.source_id or get_channel_source_id(channel.url)
|
||||
if not channel.source_id:
|
||||
channel_import_logger.warning(f'Unable to fetch source_id for {channel.url}')
|
||||
else:
|
||||
session.commit()
|
||||
save_channels_config.activate_switch()
|
||||
# Fetched outside any transaction: this asks the remote site.
|
||||
source_id = get_channel_source_id(url)
|
||||
if not source_id:
|
||||
channel_import_logger.warning(f'Unable to fetch source_id for {url}')
|
||||
return
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
Channel.find_by_id(session, channel_id).source_id = source_id
|
||||
save_channels_config.activate_switch()
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to get Channel source id of id={channel_id}', exc_info=e)
|
||||
|
||||
|
|
|
|||
|
|
@ -196,10 +196,11 @@ async def test_wrol_mode(async_client, simple_channel, simple_video, wrol_mode_f
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_video_extras(async_client, simple_channel, video_factory):
|
||||
async def test_api_video_extras(async_client, test_session, simple_channel, video_factory):
|
||||
"""Can fetch extra data about a video (comments/captions)."""
|
||||
info_json = {'duration': 5, 'epoch': 12345, 'comments': [{'some': 'comments'}]}
|
||||
video = video_factory(simple_channel.id, with_info_json=info_json, with_caption_file=True)
|
||||
test_session.commit()
|
||||
|
||||
request, response = await async_client.get(f'/api/videos/{video.file_group_id}/captions')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
|
|
|
|||
|
|
@ -38,10 +38,9 @@ async def get_zims(request: Request):
|
|||
summary='Delete all Zim files',
|
||||
)
|
||||
async def delete_zims(request: Request, zim_ids: str):
|
||||
session = request.ctx.session
|
||||
|
||||
zim_ids = [int(i) for i in zim_ids.split(',')]
|
||||
lib.delete_zims(session, zim_ids)
|
||||
with request.ctx.db.write() as session:
|
||||
lib.delete_zims(session, zim_ids)
|
||||
return response.empty()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -347,3 +347,22 @@ async def test_zim_writes_wait_for_a_concurrent_writer(async_client, test_sessio
|
|||
_, response = await async_client.delete('/api/zim/subscribe/1')
|
||||
|
||||
assert response.status_code in (HTTPStatus.CREATED, HTTPStatus.NO_CONTENT), response.body
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_zim_waits_for_a_concurrent_writer(async_client, test_session, test_zim):
|
||||
"""Deleting a Zim must delete its row as well as its file, even while something else is writing. It wrote
|
||||
in the request's deferred transaction, so its commit in the response middleware failed ("database is
|
||||
locked") after the file was deleted and the response was sent."""
|
||||
zim_id, zim_path = test_zim.id, test_zim.path
|
||||
test_session.commit()
|
||||
db_file = test_session.get_bind().url.database
|
||||
|
||||
with production_like_sessions(test_session) as maker:
|
||||
with write_lock_held_briefly(db_file, seconds=3):
|
||||
_, response = await async_client.delete(f'/api/zim/{zim_id}')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.body
|
||||
|
||||
assert not zim_path.exists()
|
||||
with maker() as session:
|
||||
assert session.query(Zim).filter_by(id=zim_id).one_or_none() is None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue