Skip to content

Commit ebf1dca

Browse files
committed
Backport:
Fix bug #1413192, fix seg fault in bsddb if a txn was deleted before the env.
1 parent 88f669a commit ebf1dca

5 files changed

Lines changed: 37 additions & 3 deletions

File tree

Lib/bsddb/test/test_1413192.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# http://python.org/sf/1413192
3+
#
4+
# This test relies on the variable names, see the bug report for details.
5+
# The problem was that the env was deallocated prior to the txn.
6+
7+
from bsddb import db
8+
9+
env_name = '.'
10+
11+
env = db.DBEnv()
12+
env.open(env_name, db.DB_CREATE | db.DB_INIT_TXN)
13+
the_txn = env.txn_begin()
14+
15+
map = db.DB(env)
16+
map.open('xxx.db', "p", db.DB_HASH, db.DB_CREATE, 0666, txn=the_txn)

Lib/bsddb/test/test_all.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def testPrintVersions(self):
4646

4747

4848
def suite():
49+
try:
50+
# this is special, it used to segfault the interpreter
51+
import test_1413192
52+
except:
53+
pass
54+
4955
test_modules = [
5056
'test_associate',
5157
'test_basics',

Lib/test/test_bsddb3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222

2323

2424
def suite():
25+
try:
26+
# this is special, it used to segfault the interpreter
27+
import bsddb.test.test_1413192
28+
except:
29+
pass
30+
2531
test_modules = [
2632
'test_associate',
2733
'test_basics',

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Core and builtins
208208
Extension Modules
209209
-----------------
210210

211+
- Bug #1413192, fix seg fault in bsddb if a transaction was deleted
212+
before the env.
213+
211214
- Bug #1402308, (possible) segfault when using mmap.mmap(-1, ...)
212215

213216
- Bug #1400822, _curses over{lay,write} doesn't work when passing 6 ints.

Modules/_bsddb.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ typedef struct {
265265
typedef struct {
266266
PyObject_HEAD
267267
DB_TXN* txn;
268+
PyObject *env;
268269
#ifdef HAVE_WEAKREF
269270
PyObject *in_weakreflist; /* List of weak references */
270271
#endif
@@ -921,6 +922,8 @@ newDBTxnObject(DBEnvObject* myenv, DB_TXN *parent, int flags)
921922
DBTxnObject* self = PyObject_New(DBTxnObject, &DBTxn_Type);
922923
if (self == NULL)
923924
return NULL;
925+
Py_INCREF(myenv);
926+
self->env = (PyObject*)myenv;
924927
#ifdef HAVE_WEAKREF
925928
self->in_weakreflist = NULL;
926929
#endif
@@ -931,11 +934,10 @@ newDBTxnObject(DBEnvObject* myenv, DB_TXN *parent, int flags)
931934
#else
932935
err = txn_begin(myenv->db_env, parent, &(self->txn), flags);
933936
#endif
934-
/* TODO add a weakref(self) to the self->myenvobj->open_child_weakrefs
935-
* list so that a DBEnv can refuse to close without aborting any open
936-
* open DBTxns and closing any open DBs first. */
937937
MYDB_END_ALLOW_THREADS;
938938
if (makeDBError(err)) {
939+
Py_DECREF(self->env);
940+
PyObject_Del(self);
939941
self = NULL;
940942
}
941943
return self;
@@ -966,6 +968,7 @@ DBTxn_dealloc(DBTxnObject* self)
966968
}
967969
#endif
968970

971+
Py_DECREF(self->env);
969972
PyObject_Del(self);
970973
}
971974

0 commit comments

Comments
 (0)