From f4fec762575932095124e7270be1a9d82143c388 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 18 Feb 2025 11:44:26 -0800 Subject: [PATCH 1/2] checkpoint: Fix memory leak in checkpoint serializer - Packer instances can't be kept in memory as they retain memory after used --- .../langgraph/checkpoint/serde/jsonplus.py | 17 ++++------------- libs/checkpoint/pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py index ff5a91f5d..a3ee079d1 100644 --- a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py +++ b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py @@ -5,6 +5,7 @@ import json import pathlib import re from collections import deque +from collections.abc import Sequence from datetime import date, datetime, time, timedelta, timezone from enum import Enum from inspect import isclass @@ -16,13 +17,13 @@ from ipaddress import ( IPv6Interface, IPv6Network, ) -from typing import Any, Callable, Optional, Sequence, Union, cast +from typing import Any, Callable, Optional, Union, cast from uuid import UUID +from zoneinfo import ZoneInfo import msgpack # type: ignore[import-untyped] from langchain_core.load.load import Reviver from langchain_core.load.serializable import Serializable -from zoneinfo import ZoneInfo from langgraph.checkpoint.serde.base import SerializerProtocol from langgraph.checkpoint.serde.types import SendProtocol @@ -502,15 +503,5 @@ def _msgpack_ext_hook(code: int, data: bytes) -> Any: return -ENC_POOL: deque[msgpack.Packer] = deque(maxlen=32) - - def _msgpack_enc(data: Any) -> bytes: - try: - enc = ENC_POOL.popleft() - except IndexError: - enc = msgpack.Packer(default=_msgpack_default) - try: - return enc.pack(data) - finally: - ENC_POOL.append(enc) + return msgpack.packb(data, default=_msgpack_default) diff --git a/libs/checkpoint/pyproject.toml b/libs/checkpoint/pyproject.toml index 2f137faad..9610d8fc5 100644 --- a/libs/checkpoint/pyproject.toml +++ b/libs/checkpoint/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langgraph-checkpoint" -version = "2.0.15" +version = "2.0.16" description = "Library with base interfaces for LangGraph checkpoint savers." authors = [] license = "MIT" From f3a0cbf294f63fa502ac332096090f04b914cf00 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 18 Feb 2025 11:46:48 -0800 Subject: [PATCH 2/2] Format --- libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py index a3ee079d1..4bd0839c2 100644 --- a/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py +++ b/libs/checkpoint/langgraph/checkpoint/serde/jsonplus.py @@ -19,11 +19,11 @@ from ipaddress import ( ) from typing import Any, Callable, Optional, Union, cast from uuid import UUID -from zoneinfo import ZoneInfo import msgpack # type: ignore[import-untyped] from langchain_core.load.load import Reviver from langchain_core.load.serializable import Serializable +from zoneinfo import ZoneInfo from langgraph.checkpoint.serde.base import SerializerProtocol from langgraph.checkpoint.serde.types import SendProtocol