Skip to content

Tag/domain locks never released after the first request per RoadRunner worker (FlockStore): short URL creation hangs forever #2699

Open

Description

@mkhattak-bos

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

  • PersistenceShortUrlRelationResolver registers itself as a postFlush listener 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.
  • CloseDbConnectionMiddleware closes the EntityManager after every request, and ReopeningEntityManager then builds a new one through EntityManagerFactory (new EntityManager($conn, $config)). With ORM 3 and DBAL 4 that new EntityManager gets a brand-new EventManager, 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), the ttl: 5 passed to createLock() is ignored (putOffExpiration() is a no-op), and acquire(blocking: true) is an unbounded flock(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:

  1. Run shlinkio/shlink:5.1.8 with PostgreSQL and WEB_WORKER_NUM=2 (no REDIS_SERVERS).
  2. Create, one at a time: v1 with tags [tag-one], v2 with [tag-two], v3 with [tag-three], v4 with [tag-three].
  3. v4 hangs. /proc/locks in the container shows the idle worker that created tag-three holding an exclusive FLOCK on data/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 a finally, so exceptions such as a non-unique slug release them too).
  • Sort tag names before locking them.
  • Consider releasing after COMMIT instead of in postFlush (which runs before COMMIT): a concurrent request can otherwise miss the uncommitted tag and fail with a unique violation on tags.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.

Activity

  1. acelaya commented on Nov 25, 2026

    @acelaya
    Member

    Can you ask the AI that wrote this to write it with less lines? thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions