Skip to content

Encoding exceptions - segfault #727

Closed

Description

@regnarg

Question

I tried to make serializable exceptions using:

import msgspec
class TaggedException(msgspec.Struct, Exception, tag=True): pass

However, this reliably segfaults Python (CPython 3.12.5, msgspec 0.18.6).

I presume there is some kind of incompatibility between the C-level layout of Struct objects and Exception objects? Are there any good alternatives for encoding exception objects? (which seems super usefil when implementing APIs) Maybe this cannot be fixed. But maybe it should at least throw an error instead of crashing the interpreter?

Activity

  1. provinzkraut commented on Aug 28, 2024

    @provinzkraut
    Member

    I usually do something like this:

    class EncodableException(Exception):
      def to_dict(self) -> dict[str, Any]:
       ...
    
    
    def enc_hook(value: Any) -> Any:
      if isinstance(value, EncodableException):
        return value.to_dict()
      raise TypeError()

    and then use this enc_hook whenever I'm encoding stuff that might contain one of those exceptions.

  2. deleted a comment from on Aug 28, 2024
  3. jcrist commented on Oct 14, 2024

    @jcrist
    Member

    Thanks for opening this. In prior versions msgspec had a check preventing mixing Struct classes with other builtin types - this check was broken on Python 3.12 but is now fixed in #745. The above example now errors nicely rather than segfaulting. If you want to encode exceptions I recommend following a pattern like the one @provinzkraut outlined above rather than defining an Exception type that is also a Struct.

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