Skip to content

How deep do exception safety guarantees need to go in FFI/unsafe code? #267

Open
@Michael-F-Bryan

Description

@Michael-F-Bryan

If I create an extern "C" function which will call a method on some other type and that function triggers a panic, is it enough to just wrap the call with std::panic::catch_unwind() and translate it to an error code?

My thinking is that the object could be messed up internally and I should use poisoning to make sure it can never be accessed again, which was my solution in Michael-F-Bryan/thin-trait-objects#2.

But all unsafe code should already ensure it won't trigger UB in the face of panics so exception safety is already guaranteed and further poisoning is overkill... isn't it? Or should unsafe code be overly conservative and try to protect users from implementations which are accidentally not Maximally Exception Safe, to use the terminology from the nomicon?

I feel like my options are to,

  1. Add a W: UnwindSafe bound to FileHandle::for_writer() (which isn't overly ergonomic because lots of types are !UnwindSafe) and just wrap panicking calls in std::panic::catch_unwind(), allowing people to have logic errors or bugs when they try to use the object later on
  2. Skip the UnwindSafe bound and neuter/poison the FileHandle the moment a panic occurs

(original question was asked in Michael-F-Bryan/thin-trait-objects#1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-unwindTopic: related to unwinding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions