Action execution fails with KeyError '_cls' when parameters contain '_ref' keys
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6.5k
- Forks
- 787
- PR merge metrics
- No merged PRs in 30d
Description
SUMMARY
Action execution fails with KeyError: '_cls' when action parameters
contain dictionary keys named _ref.
This occurs during execution scheduling (before the action runs) and
results in a 500 Internal Server Error. The issue appears to be caused
by MongoEngine dereference logic misinterpreting dictionaries containing
_ref as document references and attempting to access a missing _cls
field.
Renaming _ref to another key (e.g. object_ref) resolves the issue.
STACKSTORM VERSION
st2 3.9.0, on Python 3.8.17
OS, environment, install method
- OS: Red Hat Enterprise Linux 8.10 (Ootpa)
- Environment: Virtual machine (VMware)
- Install method: Official StackStorm one-line install script
Steps to reproduce the problem
- Create a simple python-script action:
Action metadata
name: test_ref_issue
runner_type: python-script
entry_point: test.py
parameters:
data:
type: object
required: true
Action code
from st2common.runners.base_action import Action
class TestAction(Action):
def run(self, data):
return data
- Execute the action with a payload containing
_ref:
{
"data": {
"_ref": "network/test",
"network": "10.0.0.0/24"
}
}
Expected Results
- Action execution should be successfully scheduled
- Parameters should be passed through unchanged
- Action should run and return the provided input data
Actual Results
- Execution fails during scheduling (before the action runs)
- StackStorm returns a
500 Internal Server Error - The action is never executed
Error message:
KeyError: '_cls'
Relevant stack trace excerpt:
mongoengine/dereference.py
reference_map.setdefault(get_document(item["_cls"]), set()).add(
KeyError: '_cls'
Additional Observations
- The issue occurs even with minimal payloads containing
_ref - The issue also occurs when
_refappears in nested dictionaries - Removing
_refallows execution to succeed - Renaming
_refto another key (e.g.object_ref) resolves the
issue - Other fields such as
network,comment, etc. do not cause issues - This suggests MongoEngine is incorrectly treating
_ref-containing
dicts as document references during execution persistence
Workaround
Rename _ref keys before passing data into StackStorm (e.g. _ref →
object_ref), and convert back when needed before calling external APIs
(e.g. Infoblox).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with the provided Python action and payload, then trace execution scheduling into persistence and the MongoEngine dereference logic shown in mongoengine/dereference.py. Verify handling of nested dictionaries containing _ref without _cls; done means scheduling succeeds, parameters remain unchanged, and the action returns the input data without a 500 error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100