The patch for bpo-3999 was rejected because Python internal state may be corrupted when the SIGSEGV signal is raised. This is not the case of this issue. gdbm fatal function is called when Python is in consistent state. So we free to use any Python C-API. But internal state of concrete GDBM_FILE may be corrupted, so we shouldn't use it after handling fatal error. This cause a leak, but I think that a leak with an exception is better than just a crash.
May be different type of exception should be raised. May be we need FatalError that inherits from BaseException.
Other external libraries used by the stdlib also can crash, and perhaps crashes can be converted to exceptions. This issue is only first in the series.
Other external libraries used by the stdlib also can crash, and perhaps crashes can be converted to exceptions. This issue is only first in the series.
I don't think that it's a good practice to try to workaround bugs. IMO
it's better to modify libraries directly to allow users of the library
to handle correctly errors.
The fatal_func parameter is deprecated now, so this way should no longer be used. It is expected that new gdbm versions better handle errors and do not need such function.
The tests, added in dbm_gdbm_fatal_error.patch, fail with unpatched code because it raises different exceptions or do not raise exceptions. But they do not crash. The original example still crashes. The difference is that it uses empty database. Changing the tests to use empty database makes them crashing.
We can only hope that crashes will be fixed in upstream.
I have found that using the GDBM_NOMMAP flag prevents these crashes. But it can impact performance and/or memory consumption. I think we cannot expect more.
Activity
serhiy-storchaka commented on Jul 22, 2014
It is possible to crash Python by breaking opened gdbm database.
Proposed patch tries to convert fatal gdbm into regular exception or in Python fatal error (which at least produces traceback).
BreamoreBoy commented on Feb 14, 2015
Would somebody please review Serhiy's patch.
serhiy-storchaka commented on Feb 14, 2015
Oh, Mark, please stop shaking up bug tracker.
vstinner commented on Mar 31, 2015
I would prefer to avoid setgmp/longjmp, it's kind of a hack. It's maybe more a design issue in the gdbm library to report errors.
I proposed a generic signal handler using setjmp/longjmp to convert SIGSEGV to regular Python exceptions, but it was rejected: issue bpo-3999.
vstinner commented on Mar 31, 2015
I agree: please stop posting useless messages, and review patches instead.
serhiy-storchaka commented on Apr 1, 2015
The patch for bpo-3999 was rejected because Python internal state may be corrupted when the SIGSEGV signal is raised. This is not the case of this issue. gdbm fatal function is called when Python is in consistent state. So we free to use any Python C-API. But internal state of concrete GDBM_FILE may be corrupted, so we shouldn't use it after handling fatal error. This cause a leak, but I think that a leak with an exception is better than just a crash.
May be different type of exception should be raised. May be we need FatalError that inherits from BaseException.
Other external libraries used by the stdlib also can crash, and perhaps crashes can be converted to exceptions. This issue is only first in the series.
vstinner commented on Apr 1, 2015
2015-04-01 10:39 GMT+02:00 Serhiy Storchaka <report@bugs.python.org>:
I don't think that it's a good practice to try to workaround bugs. IMO
it's better to modify libraries directly to allow users of the library
to handle correctly errors.
furkanonder commented on May 25, 2025
The issue still exists; I can reproduce it on Python 3.15
serhiy-storchaka commented on Jun 1, 2025
The fatal_func parameter is deprecated now, so this way should no longer be used. It is expected that new gdbm versions better handle errors and do not need such function.
The tests, added in dbm_gdbm_fatal_error.patch, fail with unpatched code because it raises different exceptions or do not raise exceptions. But they do not crash. The original example still crashes. The difference is that it uses empty database. Changing the tests to use empty database makes them crashing.
We can only hope that crashes will be fixed in upstream.
serhiy-storchaka commented on Jun 1, 2025
I have found that using the GDBM_NOMMAP flag prevents these crashes. But it can impact performance and/or memory consumption. I think we cannot expect more.
We can add support for that flag.
7 remaining items
Revert "pythongh-66234: Add flag to disable the use of mmap in dbm.gnu (
serhiy-storchaka commented on Jul 22, 2025
Sad. So it doesn't work. Then there's no reason to add this flag.
pythongh-66234: Add flag to disable the use of mmap in dbm.gnu (pytho…
pythongh-66234: Add flag to disable the use of mmap in dbm.gnu (pytho…
Revert "gh-66234: Add flag to disable the use of mmap in dbm.gnu (GH-…