pg-extend-rs icon indicating copy to clipboard operation
pg-extend-rs copied to clipboard

`panic!`ing twice crashes PG with "Illegal instruction"

Open auterium opened this issue 5 years ago • 7 comments

OS

Ubuntu 18.04 (Windows Subsystem for Linux)

Code

panicking example package

Issue

Rust panic!() is caught and handled properly on the first call, but if called again, the server crashes: image

Here's PG logs: image

That little extra weird stuff

This also happens when using error!() macro, but it requires 3 executions: image

Here;s PG logs: image

auterium avatar Dec 31 '19 16:12 auterium

Error, fatal and panic all cause PG to abort the transaction, where panic will cause a full restart. The illegal obstruction is concerning though.

Is this a double panic? Or is it a panic, and then a different transaction also panicking?

bluejekyll avatar Dec 31 '19 18:12 bluejekyll

Unless psql is not setting AUTOCOMMIT then yes, they are separate transactions panicking

auterium avatar Dec 31 '19 18:12 auterium

This is happening on Windows? I wonder if this behavior can be reproduced on a vanilla Linux box.

bluejekyll avatar Dec 31 '19 18:12 bluejekyll

Same error happens in a Docker container running PG10 image

auterium avatar Dec 31 '19 18:12 auterium

I haven’t looked into this, yet, but what is the expected behavior of Postgres when it panics? It might do exactly this. It would be interesting if we had a test case that showed this with and without pg-extend. That would require a vanilla C extension I think.

bluejekyll avatar Jan 01 '20 04:01 bluejekyll

Looking at PG source code, I found this: image

The important part is:

If elevel >= ERROR, the call will not return; we try to inform the compiler of that via pg_unreachable()

So, if the log level is ERROR or higher, there will be a different behaviour (no return). Perhaps this might explain the weird behaviour?

auterium avatar Jan 02 '20 15:01 auterium

Yes. That’s what I was trying to explain. Error and higher log statements have the side effect of killing the transaction/process/server, depending on which you use.

It’s really ugly imo, but that’s the way it works.

bluejekyll avatar Jan 02 '20 16:01 bluejekyll