From 4fa2a6e63e4dd2b1eb03906f00a1a732639a7e7a Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 17 Feb 2022 07:29:06 -0500 Subject: [PATCH] Fix memory leak in webhooks not deallocating locks --- discord/webhook/async_.py | 3 ++- discord/webhook/sync.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index d1ed1ade0..fac601f32 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -32,6 +32,7 @@ import re from urllib.parse import quote as urlquote from typing import Any, Dict, List, Literal, NamedTuple, Optional, TYPE_CHECKING, Tuple, Union, overload from contextvars import ContextVar +import weakref import aiohttp @@ -95,7 +96,7 @@ class AsyncDeferredLock: class AsyncWebhookAdapter: def __init__(self): - self._locks: Dict[Any, asyncio.Lock] = {} + self._locks: weakref.WeakValueDictionary[Any, asyncio.Lock] = weakref.WeakValueDictionary() async def request( self, diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index 5c85a0365..3f7cd6018 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -38,6 +38,7 @@ import re from urllib.parse import quote as urlquote from typing import Any, Dict, List, Literal, Optional, TYPE_CHECKING, Tuple, Type, TypeVar, Union, overload +import weakref from .. import utils from ..errors import InvalidArgument, HTTPException, Forbidden, NotFound, DiscordServerError @@ -91,7 +92,7 @@ class DeferredLock: class WebhookAdapter: def __init__(self): - self._locks: Dict[Any, threading.Lock] = {} + self._locks: weakref.WeakValueDictionary[Any, threading.Lock] = weakref.WeakValueDictionary() def request( self,