Skip to content

Commit

Permalink
Merge pull request #21 from ZeroIntensity/fix-leak
Browse files Browse the repository at this point in the history
Fix reference leak in `genwrapper_next`
  • Loading branch information
ZeroIntensity authored Jun 27, 2024
2 parents 1534cfd + 0caf7d8 commit 5e5147b
Show file tree
Hide file tree
Showing 6 changed files with 1,428 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix coroutine iterator reference leak.

## [1.0.0] - 2024-06-24

- Initial release.
2 changes: 1 addition & 1 deletion include/vendor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#define PYAWAITABLE_MAJOR_VERSION 1
#define PYAWAITABLE_MINOR_VERSION 0
#define PYAWAITABLE_MICRO_VERSION 0
#define PYAWAITABLE_MICRO_VERSION 1
#define PYAWAITABLE_RELEASE_LEVEL 0xF

#ifdef PYAWAITABLE_PYAPI
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(
name="pyawaitable",
license="MIT",
version = "1.0.0",
version = "1.0.1",
ext_modules=[
Extension(
"_pyawaitable",
Expand Down
5 changes: 5 additions & 0 deletions src/_pyawaitable/genwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ genwrapper_next(PyObject *self)
// Coro is done
if (!cb->callback)
{
Py_DECREF(g->gw_current_await);
g->gw_current_await = NULL;
return genwrapper_next(self);
}
Expand All @@ -204,6 +205,8 @@ genwrapper_next(PyObject *self)
{
return NULL;
}

Py_DECREF(g->gw_current_await);
g->gw_current_await = NULL;
return genwrapper_next(self);
}
Expand All @@ -212,6 +215,7 @@ genwrapper_next(PyObject *self)
{
// Coroutine is done, but with a result.
// We can disregard the result if theres no callback.
Py_DECREF(g->gw_current_await);
g->gw_current_await = NULL;
PyErr_Clear();
return genwrapper_next(self);
Expand Down Expand Up @@ -280,6 +284,7 @@ genwrapper_next(PyObject *self)
}

cb->done = true;
Py_DECREF(g->gw_current_await);
g->gw_current_await = NULL;
return genwrapper_next(self);
}
Expand Down
19 changes: 13 additions & 6 deletions src/pyawaitable/pyawaitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define PYAWAITABLE_MAJOR_VERSION 1
#define PYAWAITABLE_MINOR_VERSION 0
#define PYAWAITABLE_MICRO_VERSION 0
#define PYAWAITABLE_MICRO_VERSION 1
/* Per CPython Conventions: 0xA for alpha, 0xB for beta, 0xC for release candidate or 0xF for final. */
#define PYAWAITABLE_RELEASE_LEVEL 0xF

Expand All @@ -21,7 +21,8 @@ typedef struct _pyawaitable_abi
PyObject *,
PyObject *,
awaitcallback,
awaitcallback_err);
awaitcallback_err
);
void (*cancel)(PyObject *);
int (*set_result)(PyObject *, PyObject *);
int (*save)(PyObject *, Py_ssize_t, ...);
Expand All @@ -35,7 +36,8 @@ typedef struct _pyawaitable_abi
const char *fmt,
awaitcallback,
awaitcallback_err,
...);
...
);
} PyAwaitableABI;

#ifdef PYAWAITABLE_THIS_FILE_INIT
Expand Down Expand Up @@ -85,7 +87,8 @@ pyawaitable_init()
{
PyErr_SetString(
PyExc_RuntimeError,
"pyawaitable_init() can only be called in a file with a PYAWAITABLE_THIS_FILE_INIT #define");
"pyawaitable_init() can only be called in a file with a PYAWAITABLE_THIS_FILE_INIT #define"
);
return -1;
}

Expand All @@ -106,9 +109,13 @@ pyawaitable_init()
#define PyAwaitable_AwaitFunction pyawaitable_await_function
#endif

static int pyawaitable_vendor_init(PyObject *mod)
static int
pyawaitable_vendor_init(PyObject *mod)
{
PyErr_SetString(PyExc_SystemError, "cannot use pyawaitable_vendor_init from an installed version, use pyawaitable_init instead!");
PyErr_SetString(
PyExc_SystemError,
"cannot use pyawaitable_vendor_init from an installed version, use pyawaitable_init instead!"
);
return -1;
}

Expand Down
Loading

0 comments on commit 5e5147b

Please sign in to comment.