🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix: free up tokenizer at worker shutdown (#3041)
Max Deichmannb8ed9d681bac
diff --git a/worker/src/features/tokenisation/usage.ts b/worker/src/features/tokenisation/usage.tsindex 59d37bebd7..cea9c48802 100644--- a/worker/src/features/tokenisation/usage.ts+++ b/worker/src/features/tokenisation/usage.ts@@ -182,6 +182,12 @@ interface Tokenizer {}const cachedTokenizerByModel: Tokenizer = {};+export function freeAllTokenizers() {+ Object.values(cachedTokenizerByModel).forEach((tokenizer) => {+ tokenizer.free();+ });+}+function isString(value: unknown): value is string {return typeof value === "string";}diff --git a/worker/src/utils/shutdown.ts b/worker/src/utils/shutdown.tsindex e91e359c31..5f38518fe5 100644--- a/worker/src/utils/shutdown.ts+++ b/worker/src/utils/shutdown.ts@@ -11,6 +11,7 @@ import { setSigtermReceived } from "../features/health";import { server } from "../index";import { legacyIngestionExecutor } from "../queues/legacyIngestionQueue";import { cloudUsageMeteringJobExecutor } from "../queues/cloudUsageMeteringQueue";+import { freeAllTokenizers } from "../features/tokenisation/usage";export const onShutdown: NodeJS.SignalsListener = async (signal) => {logger.info(`Received ${signal}, closing server...`);@@ -41,5 +42,8 @@ export const onShutdown: NodeJS.SignalsListener = async (signal) => {redis?.disconnect();logger.info("Redis connection has been closed.");+ freeAllTokenizers();+ logger.info("All tokenizers are cleaned up from memory.");+logger.info("Shutdown complete, exiting process...");};