Fix race condition in subscription cleanup and add log size limit for memory management
Co-authored-by: ajnart <49837342+ajnart@users.noreply.github.com>
This commit is contained in:
1 parent
8b7d849cd0
commit
58a2c508c6
2 files changed
+51
-28
No files matched your search
@@ -142,37 +142,47 @@ export const dockerRouter = createTRPCRouter({
|
|||||||
)
|
)
|
||||||
.subscription(({ input }) => {
|
.subscription(({ input }) => {
|
||||||
return observable<string>((emit) => {
|
return observable<string>((emit) => {
|
||||||
let cleanup: (() => void) | undefined;
|
let cleanupFn: (() => void) | undefined;
|
||||||
|
let isSubscribed = true;
|
||||||
|
|
||||||
streamContainerLogsAsync(
|
const initializeStream = async () => {
|
||||||
input.id,
|
try {
|
||||||
input.tail ?? 200,
|
cleanupFn = await streamContainerLogsAsync(
|
||||||
(data) => {
|
input.id,
|
||||||
emit.next(data);
|
input.tail ?? 200,
|
||||||
},
|
(data) => {
|
||||||
(err) => {
|
if (isSubscribed) {
|
||||||
emit.error(
|
emit.next(data);
|
||||||
new TRPCError({
|
}
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
},
|
||||||
message: err.message,
|
(err) => {
|
||||||
}),
|
if (isSubscribed) {
|
||||||
|
emit.error(
|
||||||
|
new TRPCError({
|
||||||
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
|
message: err.message,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
);
|
);
|
||||||
},
|
} catch (err) {
|
||||||
)
|
if (isSubscribed) {
|
||||||
.then((cleanupFn) => {
|
emit.error(
|
||||||
cleanup = cleanupFn;
|
new TRPCError({
|
||||||
})
|
code: "NOT_FOUND",
|
||||||
.catch((err) => {
|
message: err instanceof Error ? err.message : "Container not found",
|
||||||
emit.error(
|
}),
|
||||||
new TRPCError({
|
);
|
||||||
code: "NOT_FOUND",
|
}
|
||||||
message: err instanceof Error ? err.message : "Container not found",
|
}
|
||||||
}),
|
};
|
||||||
);
|
|
||||||
});
|
void initializeStream();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
cleanup?.();
|
isSubscribed = false;
|
||||||
|
cleanupFn?.();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -38,12 +38,25 @@ export const DockerLogsModal = createModal<DockerLogsModalInnerProps>(({ innerPr
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Maximum log size to prevent memory issues (500KB)
|
||||||
|
const MAX_LOG_SIZE = 500 * 1024;
|
||||||
|
|
||||||
// Subscribe to streaming logs
|
// Subscribe to streaming logs
|
||||||
clientApi.docker.subscribeLogs.useSubscription(
|
clientApi.docker.subscribeLogs.useSubscription(
|
||||||
{ id: innerProps.id, tail: innerProps.tail ?? 200 },
|
{ id: innerProps.id, tail: innerProps.tail ?? 200 },
|
||||||
{
|
{
|
||||||
onData(data) {
|
onData(data) {
|
||||||
setLogs((prev) => prev + data);
|
setLogs((prev) => {
|
||||||
|
const newLogs = prev + data;
|
||||||
|
// Truncate from the beginning if logs exceed maximum size
|
||||||
|
if (newLogs.length > MAX_LOG_SIZE) {
|
||||||
|
const truncated = newLogs.slice(-MAX_LOG_SIZE);
|
||||||
|
// Try to start from a newline to avoid partial lines
|
||||||
|
const firstNewline = truncated.indexOf("\n");
|
||||||
|
return firstNewline > 0 ? truncated.slice(firstNewline + 1) : truncated;
|
||||||
|
}
|
||||||
|
return newLogs;
|
||||||
|
});
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
},
|
},
|
||||||
onError(err) {
|
onError(err) {
|
||||||
|
|||||||
Reference in new issue
Block a user