Skip to content

Removing lock inside QueueHandler #163236

Backlog
Backlog

Description

@johng

Currently QueueHandler takes a lock before formatting and emitting to the internal logging queue. In free-threading this removes any scaling opportunities you might have for under heavy logging pressure from multiple threads.

When benchmarking this I found scaling can easily go below 1x due to contention on the lock. I tested a couple variants. The "cookbook" the recommend example which minimises the critical section and a less optimised "fmt" variant that does formatting in the critical section.

q = queue.SimpleQueue()
logger.addHandler(QueueHandler(q))               # no formatter on this one

file_handler = FileHandler("app.log")
file_handler.setFormatter(Formatter("%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s"))
QueueListener(q, file_handler).start()           # full formatting happens here, in one thread

The "fmt" variant moves the formatter to the QueueHandler:

qh = QueueHandler(q)
qh.setFormatter(Formatter("%(asctime)s %(levelname)s %(threadName)s %(name)s %(message)s"))
logger.addHandler(qh)                            # full formatting happens in every logging thread

Benchmarks on a Xeon 8358 with 16 vCPUs, with PGO+LTO optimisations for pure logging throughput.

Threads Cookbook, stock Cookbook, patched Cookbook gain fmt, stock fmt, patched fmt gain
1 1.00x (72k/s) 1.00x (69k/s) 1.0x 1.00x (60k/s) 1.00x (56k/s) 0.9x
2 0.91–0.92x 1.51–1.54x 1.6x 0.95x 1.18–1.19x 1.2x
4 0.86–0.87x 2.23–2.25x 2.5x 0.85–0.87x 2.60–2.65x 2.8x
6 0.85–0.87x 3.19–3.55x 3.8x 0.86–0.87x 3.69–3.79x 4.0x
8 0.77–0.80x 3.73–3.79x 4.6x 0.76x 4.39–4.49x 5.5x
9 0.75–0.76x 3.45–3.68x 4.6x 0.65–0.66x 4.44–4.60x 6.4x
10 0.70–0.71x 4.08–4.29x 5.8x 0.60x 4.23–4.44x 6.8x
11 0.60–0.64x 4.46–4.61x 7.1x 0.58–0.60x 4.35–4.77x 7.2x
12 0.59–0.61x 4.73–4.76x 7.6x 0.58–0.59x 4.41–4.90x 7.5x
13 0.58–0.59x 5.25–5.38x 8.8x 0.56–0.57x 5.34–5.42x 8.9x
14 0.57–0.58x 5.74–5.77x 9.7x 0.53–0.54x 5.85–5.86x 10.3x
15 0.55–0.59x 6.23–6.24x 10.5x 0.12–0.22x 6.27–6.29x 34x
16 0.55–0.62x 5.20–5.47x 8.8x 0.16–0.29x 4.73–5.37x 21x

Obvious a real program would do some work between logging which would impact the results. Focusing on the "cookbook" case and adding varying amount of work we see the following results.

Gain (patched ÷ stock) 1 thr 2 thr 4 thr 8 thr 12 thr 15 thr 16 thr
none 1.0x 1.6x 2.8x 5.4x 7.7x 10.2x 8.4x
150 steps (~4 µs) 1.0x 1.2x 1.9x 3.2x 5.5x 7.4x 6.3x
600 steps (~17 µs) 1.0x 1.0x 1.6x 2.1x 3.2x 4.3x 4.5x
1,500 steps (~42 µs) 1.0x 1.0x 1.1x 1.4x 1.8x 2.3x 2.2x
6,000 steps (~169 µs) 1.0x 1.0x 1.0x 1.0x 1.0x 1.0x 1.0x
CPU-only control speedup 1.0x 2.0x 3.8x 3.5x 5.2x 6.4x 6.8x

As we increase the CPU work and reduce pressure on the logging clearly any bottleneck or gain is reduced. I think for a high throughput application such as a bursty webserver could start seeing degradation.

This was discussed before in #69854 however under the context of deadlocking and not performance. There were some concerns such as impact of subclassing, I'm wondering for that specially we could make this lock free for just the parent type.

Activity

  1. picnixz commented on Dec 6, 2026

    @picnixz
    Member

    If we don't have a lock, can't we have a race then?

  2. added
    performance
    stdlib
    type-bug
    type-feature
    and removed
    type-feature
    type-bug
    on Dec 6, 2026
  3. johng commented on Dec 6, 2026

    @johng
    ContributorAuthor

    Inside the queuehandler lock is only preparing the statements before putting the message on the queue. I think even with the lock there is no ordering guarantees however this would likely increase the risk. It is not though protecting against any corrupted messages since unlike other loggers is not emitting inside the lock too.

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

    performancestdlibtype-feature

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions