Skip to content

Conversation

@Kami
Copy link
Member

@Kami Kami commented Jun 19, 2017

@tonybaloney reported this issue (user unfriendly behavior) on Slack.

Right now, if Python runner actions returns a result which can't be serialized (e.g. non complex type such as class instance, datetime object or similar), Python runner will "silently" swallow this issue and simply return None as the result.

This is obviously very unfriendly and confusing.

The code change I included just logs a message and still returns None, but I think a better and more user-friendly approach is to simply throw a very explicit and user-friendly error message - something along the lines of "Failed to de-serialize action result, it most likely contains a non-simple type or similar, see docs at YYY on info on Python runner action results".

The thing is if we can't serialize and de-serialize the result user can't do anything with it anyway so throw makes sense because user / developer interaction is required (updating the action code so it only returns simple types).

@tonybaloney asked if we recently made a change which affects that, but AFAIK this behavior has been there since the beginning - I checked the code and we did the same thing in v1.6 as well (https://github.com/StackStorm/st2/blob/v1.6/st2actions/st2actions/runners/pythonrunner.py#L190).

Example action which reproduces this issue - https://gist.github.com/Kami/7e654186f95eda654589a171c9f90d12

action result (e.g. due to non-simple type in the result or similar).
@Kami Kami added the bug label Jun 19, 2017
@Kami
Copy link
Member Author

Kami commented Jun 19, 2017

Managed to track down old behavior @tonybaloney was described to v1.5 -
https://github.com/StackStorm/st2/blob/v1.5/st2actions/st2actions/runners/pythonrunner.py#L158.

The different was that at that point in time, we only tried to de-serialize it if user returned JSON from action, otherwise we always simply returned a string (which wasn't all that useful). This behavior was introduced in v1.6 when we introduced result tuple and ability to return status from Python runner actions.

I will look if we can replicate pre v1.6 behavior, but I still think correct long term approach would be throwing.

/cc @humblearner

(serialized as string) in case the result contains non-simple type and
we can't de-serialize it.
@Kami
Copy link
Member Author

Kami commented Jun 19, 2017

I pushed a change to utilize pre-v1.6 behavior in - 1de143a.

Now if we fail to de-serialize the result because it contains a non-simple type we simply return result as is aka serialized as a string.

Before this change (post v1.6):

id: 5947967e0640fd7382f43278
status: succeeded
parameters: None
result: 
  exit_code: 0
  result: None
  stderr: 'No handlers could be found for logger "st2.st2common.util.loader"

    '
  stdout: ''

After this change (pre v1.6 and going forward):

id: 59479dea0640fd7382f4328d
status: succeeded
parameters: None
result: 
  exit_code: 0
  result: '[{''a'': ''1''}, {''b'': ''2''}, {''h'': 5, ''c'': 3}, {''e'': <print_config.Test object at 0x7f45c758fed0>}]}'
  stderr: 'No handlers could be found for logger "st2.st2common.util.loader"

    '
  stdout: ''

else:
result = 'None'
# Failed to de-serialize return result as is aka as a string
match = re.search("'result': (.*?)$", action_result).groups()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is horrible but we have a safe-guard below - if there are no results we simply return result as is.

And as mentioned in other comment, I believe that correct long term approach would be to throw.

@Kami
Copy link
Member Author

Kami commented Jun 19, 2017

P.S. Thanks again for @tonybaloney for catching and reporting this.

@Kami Kami changed the title [RFC] Python runner should throw / log an error when failing to de-serialize action result Python runner should throw / log an error when failing to de-serialize action result Jun 19, 2017
@tonybaloney
Copy link
Contributor

👍

@Kami Kami added this to the 2.3.1 milestone Jun 19, 2017
CHANGELOG.rst Outdated
non-simple types (e.g. class instances) which can't be serialized.

In v1.6 we introduced a change when in such instances, we simply returned ``None`` as result
and didn't log anything which was confusing. (improvement)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's please include the PR# in the changelog.

Could be a good tradition to follow, since it's easier to find the change later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do - I agree it's a good idea and we should do it for all the changes :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - 21b8fa8.

@lakshmi-kannan @m4dcoder @bigmstone let's all try to go doing going forward :)

Copy link
Member

@arm4b arm4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides of the changelog, looks good 👍

…kStorm/st2 into python_runner_non_simple_type_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants