Skip to content

Instantly share code, notes, and snippets.

@joar
Created May 4, 2018 09:53
Show Gist options
  • Save joar/843142269c6cfbb5ac63f8c357599e88 to your computer and use it in GitHub Desktop.
Save joar/843142269c6cfbb5ac63f8c357599e88 to your computer and use it in GitHub Desktop.
GraphQL memory leak
async def monitoring(handler, root, info: ResolveInfo, **kwargs):
    t_start = time.perf_counter()
    result = handler(root, info, **kwargs)

    if not isawaitable(result):
        return result

    result = await result
    duration = max(time.perf_counter() - t_start, 0)

    if root is not None and hasattr(root, '_meta'):
        parent_type = root._meta.name
    else:
        parent_type = '<root>'

    operation_type = info.operation.operation

    field_name = info.field_name

    RESOLVE_LATENCY.labels(
        operation_type,
        f'{parent_type}.{field_name}',
    ).observe(duration)

    return result
middleware = MiddlewareManager(monitoring, wrap_in_promise=False)

Each time the monitoring middleware is executed, a new asyncio.Task is created, that task is then never retrieved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment