Repository navigation
Description
Shlink version
5.1.8 (the same code is on main)
How do you serve Shlink
Docker image (RoadRunner), PostgreSQL 17, no REDIS_SERVERS.
Current behavior
Some POST /rest/v3/short-urls requests with tags never return. Each one holds a RoadRunner web worker with an open DB transaction, idle right after BEGIN. Once every web worker is stuck, all requests (including redirects) fail with no free workers in the pool, wait timeout exceed, until the container is restarted. This looks like #2444, which was closed as not reproducible.
Expected behavior
Tag and domain locks are released at the end of every request, and lock waits are bounded.
Root cause
PersistenceShortUrlRelationResolverregisters itself as apostFlushlistener in its constructor, on the EventManager of the EntityManager that exists at that moment.postFlush()is the only place locks on newly created tags and domains are released.CloseDbConnectionMiddlewarecloses the EntityManager after every request, andReopeningEntityManagerthen builds a new one throughEntityManagerFactory(new EntityManager($conn, $config)). With ORM 3 and DBAL 4 that new EntityManager gets a brand-newEventManager, so the resolver's listener is gone.- So from the second create a worker handles onward,
postFlush()never runs in that worker, and the locks on any tag it creates are never released. - With
FlockStore(the default without Redis), thettl: 5passed tocreateLock()is ignored (putOffExpiration()is a no-op), andacquire(blocking: true)is an unboundedflock(LOCK_EX). Any other worker that later resolves the same tag blocks forever. - Separately: locks on new tags are taken in the order the request lists them and held until flush, so two concurrent requests creating the same new tags in opposite order can deadlock each other.
How to reproduce
Deterministic, sequential requests, no concurrency needed:
- Run
shlinkio/shlink:5.1.8with PostgreSQL andWEB_WORKER_NUM=2(noREDIS_SERVERS). - Create, one at a time:
v1with tags[tag-one],v2with[tag-two],v3with[tag-three],v4with[tag-three]. v4hangs./proc/locksin the container shows the idle worker that createdtag-threeholding an exclusive FLOCK ondata/locks/sf.tag_tag-three.*.lock, and the other worker waiting on it.
The first create in each worker works, because the listener is still attached to that worker's first EntityManager. That is probably why #2444 couldn't be reproduced.
Suggested fix
- Register the listener on the current EntityManager on every
resolveTags()/resolveDomain()call, or release the locks explicitly once the transaction ends (in afinally, so exceptions such as a non-unique slug release them too). - Sort tag names before locking them.
- Consider releasing after
COMMITinstead of inpostFlush(which runs beforeCOMMIT): a concurrent request can otherwise miss the uncommitted tag and fail with a unique violation ontags.name.
A local patch doing the first two points (re-register the listener on each call, sort tags) removed every hang across about 480 creates, concurrent and sequential. Setting REDIS_SERVERS also avoids the hang, since the Redis store honours the TTL.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
acelaya commentedon Nov 25, 2026
Can you ask the AI that wrote this to write it with less lines? thank you