`mongodb` output plugin blocks the reactor thread on every `write`, freezing all honeypot sessions
Describe the bug
src/cowrie/output/mongodb.py's Output.write() calls find_one(),
insert_one(), and update_one() directly against a pymongo
collection for every logged event. pymongo's default client is
synchronous and performs blocking network I/O. Since write() runs
directly on the Twisted reactor thread, every logged event -- across
every event type this plugin handles -- blocks the entire process,
freezing every connected honeypot session for the duration of each
MongoDB round trip (and indefinitely if MongoDB becomes unresponsive).
Same root cause as the identically-shaped bug already reported against
redis.py; the plugins this codebase already gets right (abuseipdb.py,
s3.py) use twisted.internet.threads.deferToThread for exactly this
reason.
To Reproduce Steps to reproduce the behavior:
- Enable the
output_mongodbplugin pointed at a MongoDB server (or an address that will hang rather than immediately refuse the connection). - Make the MongoDB server slow or unresponsive.
- Trigger any logged event and observe that every other connected honeypot session stops responding until the MongoDB call returns or times out.
Expected behavior A slow or unreachable MongoDB server should not freeze unrelated honeypot sessions; the blocking MongoDB calls should run off the reactor thread.
Server (please complete the following information):
- OS: This bug is OS-independent (Twisted reactor architecture, no OS-specific code involved).
- Python: Python 3.14.2
- Twisted: Twisted 26.4.0
Additional context
- Cowrie Mode: Shell (default), any protocol.
- Several event types in
write()do afind_one()lookup and then conditionallyupdate_one()based on the result (e.g.cowrie.client.version,cowrie.session.closed), so calls for the same session must still execute in the order they were submitted -- the reactor's defaultdeferToThreadpool runs several worker threads at once and would not guarantee that. The fix instead uses a dedicated single-workertwisted.python.threadpool.ThreadPool(started instart(), stopped instop()), which keeps every Mongo call off the reactor thread while still running them strictly one at a time, in submission order. - Patched file:
src/cowrie/output/mongodb.py(Output.start(): creates and starts the dedicated thread pool;Output.write(): now defers the (renamed)_write()body to that pool viatwisted.internet.threads.deferToThreadPoolinstead of running it directly;Output.stop(): stops the thread pool too).
Source: cowrie/cowrie