fix chunker holding the GIL during blocking I/O

This commit is contained in:
Marian Beermann 2017-03-30 13:09:02 +02:00
parent 8d89ee981c
commit b1b66be593
1 changed files with 7 additions and 0 deletions

View File

@ -157,6 +157,8 @@ chunker_fill(Chunker *c)
off_t offset, length; off_t offset, length;
int overshoot; int overshoot;
PyObject *data; PyObject *data;
PyThreadState *thread_state;
memmove(c->data, c->data + c->last, c->position + c->remaining - c->last); memmove(c->data, c->data + c->last, c->position + c->remaining - c->last);
c->position -= c->last; c->position -= c->last;
c->last = 0; c->last = 0;
@ -165,6 +167,8 @@ chunker_fill(Chunker *c)
return 1; return 1;
} }
if(c->fh >= 0) { if(c->fh >= 0) {
thread_state = PyEval_SaveThread();
offset = c->bytes_read; offset = c->bytes_read;
// if we have a os-level file descriptor, use os-level API // if we have a os-level file descriptor, use os-level API
n = read(c->fh, c->data + c->position + c->remaining, n); n = read(c->fh, c->data + c->position + c->remaining, n);
@ -177,6 +181,7 @@ chunker_fill(Chunker *c)
c->eof = 1; c->eof = 1;
} }
else { else {
PyEval_RestoreThread(thread_state);
// some error happened // some error happened
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
return 0; return 0;
@ -211,6 +216,8 @@ chunker_fill(Chunker *c)
posix_fadvise(c->fh, offset & ~pagemask, length - overshoot, POSIX_FADV_DONTNEED); posix_fadvise(c->fh, offset & ~pagemask, length - overshoot, POSIX_FADV_DONTNEED);
#endif #endif
PyEval_RestoreThread(thread_state);
} }
else { else {
// no os-level file descriptor, use Python file object API // no os-level file descriptor, use Python file object API