Skip to content

A webhook answers 202, but the rule does not run when st2rulesengine cannot store the trigger instance: the message is acked anyway #6416

Open

Description

@miaoti

SUMMARY

POST /v1/webhooks/<hook> answers 202, but when st2rulesengine cannot store the resulting trigger instance, the rule does not run and nothing redelivers the event. st2api only publishes the dispatch to RabbitMQ. In st2rulesengine, StagedQueueConsumer.process calls pre_ack_process, which stores the trigger instance; if that raises, it logs failed to process message and acks the message in a finally block anyway. The instance, the rule evaluation and the action execution are then lost: after the insert condition cleared, no instance or execution appeared within 30 seconds, and the dispatch queue held no unacknowledged or requeued copy.

STACKSTORM VERSION

st2 3.9.0, the newest release. The code quoted below is unchanged on master (05d45db).

OS, environment, install method

The runs used the stackstorm-k8s chart 1.1.0 on Kubernetes with the stackstorm/st2*:3.9.0 images, MongoDB 4.4 (one replica-set member), RabbitMQ 3.8.9 and Redis 7.2. st2web, st2stream, st2notifier, st2sensorcontainer and st2garbagecollector were not deployed. The steps below use st2-docker.

Steps to reproduce the problem

git clone --branch v3.9.0 https://github.com/StackStorm/st2-docker && cd st2-docker
export ST2_VERSION=3.9.0; docker compose up -d
st2c() { docker compose exec -T st2client st2 "$@"; }
mongoc() { docker compose exec -T mongo mongo st2 --quiet --eval "$1"; }
until st2c action get core.echo >/dev/null 2>&1; do sleep 5; done
KEY=$(st2c apikey create -k)
B=http://127.0.0.1/api/v1; H="St2-Api-Key: $KEY"; J='Content-Type: application/json'

# a rule on a webhook trigger that echoes the payload
REF=$(curl -s -H "$H" -H "$J" $B/rules -d '{"name":"demo","pack":"default","enabled":true,"trigger":{"type":"core.st2.webhook","parameters":{"url":"demo"}},"criteria":{},"action":{"ref":"core.echo","parameters":{"message":"{{trigger.body.msg}}"}}}' | grep -o '"ref":"core\.[0-9a-f-]*"' | cut -d'"' -f4)
until curl -sf -H "$H" $B/webhooks/demo >/dev/null; do sleep 1; done

# 1. healthy database
curl -s -o /dev/null -w '%{http_code}\n' -H "$H" -H "$J" $B/webhooks/demo -d '{"msg":"first"}'       # 202
sleep 5; st2c trigger-instance list --trigger "$REF"; st2c execution list --action core.echo           # one instance, one succeeded execution

# 2. every insert into the collection fails
mongoc 'printjson(db.runCommand({collMod:"trigger_instance_d_b", validator:{$expr:{$eq:[1,2]}}, validationLevel:"strict", validationAction:"error"}))'
curl -s -o /dev/null -w '%{http_code}\n' -H "$H" -H "$J" $B/webhooks/demo -d '{"msg":"second"}'      # still 202
sleep 5; docker compose logs st2rulesengine | grep 'failed to process message'                         # one line, body {'msg': 'second'}

# 3. validator removed
mongoc 'printjson(db.runCommand({collMod:"trigger_instance_d_b", validator:{}, validationLevel:"off"}))'
sleep 30; st2c trigger-instance list --trigger "$REF"; st2c execution list --action core.echo           # still only "first"
docker compose exec -T rabbitmq rabbitmqctl list_queues name messages_ready messages_unacknowledged | grep trigger_instances_dispatch   # 0 0

The validator stands in for any condition that makes the insert raise, for example a primary election, a full disk or a payload MongoDB cannot store (#3365). Only the validator was tested.

Expected Results

An accepted event should not be acked until it is stored, or the insert should be retried. The comment in process (consumers.py#L103) reads "At this point we will always ack a message." It came with #2643, together with a test that acks a wrong-typed payload (test_queue_consumer.py#L98-L103), which suits a message that can never be processed, like the payload of #3365. The same line also covers an insert that would succeed later, and then an event the API accepted is lost. Against that, the docstring of pre_ack_process (worker.py#L44-L47) says the two steps give "a way to not acknowledge messages", and #2643 is titled "Acknowledge MQ message only after a TriggerInstance is stored in DB". A possible fix: do not ack when pre_ack_process fails; nack with requeue and a dead-letter limit, or retry the insert.

Actual Results

In three runs on the chart, all 6 POSTs sent while the validator was active (about a second each time) returned 202, and each produced one log line like this (payload shortened):

ERROR [-] StagedQueueConsumer failed to process message: {'trigger': {... 'parameters': {'url': 'demo'}, ...}, 'payload': {... 'body': {'msg': 'second'}}, 'trace_context': <st2common.models.api.trace.TraceContext object at 0x...>}

After the validator was removed, none of the 6 had a trigger instance or an execution when listed over the next 30 seconds, and st2.trigger_instances_dispatch.rules_engine held 0 ready and 0 unacknowledged messages. The 6 POSTs sent before the validator was applied were stored and run within 3 seconds. Read again 94 minutes later, after every service had been restarted, the 6 still had no trigger instance and the other 6 were all listed.

The block above was also run once with the same images on the chart, with the docker compose commands done through kubectl: the webhook answered 202, one failed to process message line appeared, and no trigger instance or execution followed, at 34 seconds or at 5 minutes. The traceback under that line ends in mongoengine.errors.OperationError: Could not save document (Document failed validation, ...), raised while handling pymongo.errors.WriteError (code 121); another cause of a failed insert raises its own error and takes the same path.

Where

  • StagedQueueConsumer.process acks in finally. The same finally is in the other three consumers in that file (code reading; only this one was exercised). On master the method is unchanged, at L94-L108.
  • pre_ack_process stores the instance with one TriggerInstance.add_or_update call (container/utils.py#L52-L57). The 202 is returned at webhooks.py#L192.
  • git grep finds no nack, reject, requeue or dead-letter code in the tree, and st2 trigger-instance re-emit needs the id of a stored instance.

Related

Activity

  1. guzzijones commented on Nov 5, 2026

    @guzzijones
    Contributor

    The fact that we ack messages immediately is a larger issue altogether. The engines all accumulate 'tasks' in memory and there is no way to query that.
    If we ack a message we should have run the 'task'. that way messages stay on the work queue in the rabbitmq messenger service until they are actually run.
    This is a major refactor. The entire engine system is currently setup around these assumptions.

  2. guzzijones commented on Nov 5, 2026

    @guzzijones
    Contributor

    Not acking immediately will allow us to scale runners more efficiently according to work actually being worked on.

  3. guzzijones commented on Nov 21, 2026

    @guzzijones
    Contributor

    actually the queue #6158 fix does not fix this.

  4. guzzijones commented on Nov 21, 2026

    @guzzijones
    Contributor

    we need a similar rules engine fix like ththe worfklow engine fix is here actually

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions