fourthaugmentation
fourthaugmentation

Reputation: 33

How to handle "An unknown error occurred" when running a Rust program?

I am currently running cargo run and getting the most generic error, An unknown error has occurred.

When I then run cargo run --verbose I get Process didn't exit successfully: 'target/debug/ok_rust' (signal: 11) which I have no clue how to handle.

How do I debug this? What am I supposed to do? Test it with the nightly version, but the same libraries? How am I supposed to know if I'm responsible or if it's Rust?

Upvotes: 0

Views: 677

Answers (1)

Vladimir Matveev
Vladimir Matveev

Reputation: 127801

According to the error you provided, this is not a problem with tooling (that is, Cargo and rustc both work correctly) but with your program:

Process didn't exit successfully: 'target/debug/ok_rust' (signal: 11)

Signal 11 means that a segmentation fault has happened in the program. Segfaults usually happen when invalid memory is accessed, for example, when a destroyed object is read. Rust is explicitly designed to avoid segfaults; if one happens, it means that one of the unsafe blocks in your program contains an error. This unsafe block may be the one you have written yourself or it may be in one of the libraries you use.

Anyway, you need to find the exact place where the segfault happens. You can use a debugger (gdb or lldb, depending on your system) or you can add debug output to your program, with which you will likely be able to pinpoint the problematic line. Then you'll need to trace the problem back to one of the unsafe blocks. For example, if you find that the segfault happens when accessing a value through a reference, like

let x = some_struct.field;

where some_struct: &SomeStruct, then it is likely that some_struct points to an invalid object; this can only happen if some_struct was created in an unsafe block, so you need to find where some_structs originates.

Upvotes: 5

Related Questions