Skip to content

Commit e790028

Browse files
committed
Take read lock before checking if closed
This prevents a race. We also check that the database is closed in two other places, but neither of those actually read from the database.
1 parent 2f45722 commit e790028

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

extension/maxminddb.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,15 @@ static bool is_ipv6(char ip[16]) {
705705

706706
static PyObject *ReaderIter_next(PyObject *self) {
707707
ReaderIter_obj *ri = (ReaderIter_obj *)self;
708-
if (ri->reader->closed == Py_True) {
709-
PyErr_SetString(PyExc_ValueError,
710-
"Attempt to iterate over a closed MaxMind DB.");
708+
709+
if (reader_acquire_read_lock(ri->reader) != 0) {
711710
return NULL;
712711
}
713712

714-
if (reader_acquire_read_lock(ri->reader) != 0) {
713+
if (ri->reader->closed == Py_True) {
714+
reader_release_read_lock(ri->reader);
715+
PyErr_SetString(PyExc_ValueError,
716+
"Attempt to iterate over a closed MaxMind DB.");
715717
return NULL;
716718
}
717719

0 commit comments

Comments
 (0)