Isolator memory guard ignores memory outside the managed heap: host SIGKILL with no runtime error and no saved log
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Ambito
- backend, performance
Direzione di ricerca
Start with Common/Isolator.cs around the managed heap check and the trace that reports App; then read Engine/Engine.cs and Engine/Results/BacktestingResultHandler.cs:776-789 to follow error handling and log saving. The history cites #2286, #2287, and #2814, including a reverted process memory change whose reason is unknown. Done means choosing and documenting a safe response to memory outside the managed heap so a run reports the cause and preserves its logs.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Expected Behavior
When an algorithm's process memory grows past the node's RAM allocation, LEAN stops the algorithm itself with a runtime error that names memory as the cause, saves the log, and reports a stack trace, the way it does when the managed heap crosses memoryCap (Execution Security Error: Memory Usage Maxed Out).
Actual Behavior
The Isolator memory guard samples only the managed heap (GC.GetTotalMemory(false), Common/Isolator.cs:112-118). Memory allocated outside it (Python objects, numpy buffers, native libraries) is logged in the App: field of the once-a-minute Isolator.ExecuteWithTimeLimit() trace line (Common/Isolator.cs:135) but never compared to the cap. When that memory crosses the node's allocation the host kills the process (SIGKILL). The result is:
/backtests/readerror=runLauncher.sh: line 19: 8 Killed dotnet QuantConnect.Lean.Launcher.dll ...(or theAlgorithmTimeLimitManager3-minute warning joined with it),stacktracenull;- no log file:
BacktestingResultHandler.Exit()is the only placeSaveLogsruns (Engine/Results/BacktestingResultHandler.cs:776-789), so everyLog()/Debug()line of the run is lost and/backtests/read/loganswers{"logs":[],"length":-1}; Engine.TryGetOutOfMemoryErrorDetails(Engine/Engine.cs) never runs, because no managedOutOfMemoryExceptionis thrown.
Only the periodic result store (runtime statistics, every 30 s) survives, so the user sees a dead run with no error, no log and no indication that memory was the cause.
Two production cases this week, on different accounts, had this shape: on a 7987 MB node, Used stayed at or under 340 MB for the whole run while App sat near 5.4 GB and then rose by about 2 GB per minute inside one time step (5,405 -> 5,722 -> 7,593 -> 8,960 -> 10,864 MB on consecutive minute samples) until the kill, before the 10-minute time-step limit; on an 8192 MB node, Used reached 6.3 GB while App passed 11.6 GB. Neither run left a log or a message.
Reproducing the Problem
Python algorithm, QC Cloud, B8-16 node (Engine.Run(): Resource limits '8' CPUs. 16384 MB RAM.), LEAN 2.5.0.0.18194, backtest 654669611e81dabaf3b18435c484800f (project 38161606):
from AlgorithmImports import *
import numpy as np
import time
class NativeMemoryGrowth(QCAlgorithm):
def initialize(self):
self.set_start_date(2024, 1, 2)
self.set_end_date(2024, 1, 10)
self.add_equity("SPY", Resolution.MINUTE)
self.retained = []
self.bars = 0
def on_data(self, data):
self.bars += 1
# 32 MB per minute bar, page-touched so it is committed; ~0.3 s per bar
self.retained.append(np.ones(32 * 1024 * 1024, dtype=np.uint8))
time.sleep(0.3)
if self.bars % 20 == 0:
self.set_runtime_statistic("bars", str(self.bars))
self.set_runtime_statistic("retained_mb", str(32 * len(self.retained)))
self.set_runtime_statistic("app_mb", str(OS.APPLICATION_MEMORY_USED))
self.set_runtime_statistic("managed_mb", str(OS.TOTAL_PHYSICAL_MEMORY_USED))
self.log(f"bars={self.bars} retained_mb={32*len(self.retained)} app_mb={OS.APPLICATION_MEMORY_USED} managed_mb={OS.TOTAL_PHYSICAL_MEMORY_USED}")
Syslog of that run:
TRACE:: Engine.Run(): Resource limits '8' CPUs. 16384 MB RAM.
TRACE:: Isolator.ExecuteWithTimeLimit(): Used: 170, Sample: 266, App: 4936, CurrentTimeStepElapsed: 00:00.000. CPU: 54%
TRACE:: Isolator.ExecuteWithTimeLimit(): Used: 231, Sample: 267, App: 8557, CurrentTimeStepElapsed: 00:00.000. CPU: 46%
TRACE:: Isolator.ExecuteWithTimeLimit(): Used: 255, Sample: 269, App: 11367, CurrentTimeStepElapsed: 00:00.000. CPU: 56%
TRACE:: Isolator.ExecuteWithTimeLimit(): Used: 265, Sample: 273, App: 15149, CurrentTimeStepElapsed: 00:00.000. CPU: 35%
TRACE:: Isolator.ExecuteWithTimeLimit(): Used: 271, Sample: 274, App: 18672, CurrentTimeStepElapsed: 00:00.000. CPU: 45%
Nothing after that line. /backtests/read: status Runtime Error, error = [ERROR] FATAL UNHANDLED EXCEPTION:runLauncher.sh: line 19: 8 Killed dotnet QuantConnect.Lean.Launcher.dll ..., stacktrace null, runtime statistics preserved (bars 380, retained_mb 12160, app_mb 13609, managed_mb 270). /backtests/read/log: {"logs":[],"length":-1,"success":true}, although the algorithm logged a line every 20 bars.
History
#2286 reported exactly this in 2018 and #2287 (merged 2018-07-23) switched the sample to OS.ApplicationMemoryUsed; it was reverted the same day (commit 818591e1, "Revert 'Isolator will now take into account unmanaged memory usage'") with no recorded reason, and #2814 (2019) then added App to the trace line as logging only. Current master samples GC.GetTotalMemory and only logs App, so the guard is managed-heap-only. If the revert was deliberate (false kills from a process-memory measure), the reason is worth recording here before deciding the fix.
Potential Solution
Either, or both:
- In
Isolator.MonitorTask, also compareOS.ApplicationMemoryUsedto the cap: log the 80% warning on it and stop the algorithm with a message that names process memory (unmanaged / Python) as the cause, so the run ends throughHandleAlgorithmErrorwith a saved log and a stack trace. A separate, higher threshold for the process measure would keep the managed-heap spike allowance intact. - Flush
LogStoreto the results folder periodically (the result packet is already stored every 30 s), so a host kill does not lose every log line of a multi-hour run.
System Information
QC Cloud backtesting, LEAN 2.5.0.0.18188 and 2.5.0.0.18194, Python algorithms. Intercom conversations 215476470057450 and 215476469407523.
Checklist
- I have completely filled out this template
- I have confirmed that this issue exists on the current
masterbranch - I have confirmed that this is not a duplicate issue by searching issues (#2286 is the 2018 report; its fix is no longer in master)
- I have provided detailed steps to reproduce the issue
- Lingua principale
- C#
- Stelle
- 22.6k
- Fork
- 5.4k
- Merge medio
- 11h 39m
- PR unite (30g)
- 104
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di QuantConnect/Lean
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
QuantConnect/Lean#10216 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
QuantConnect/Lean#10206 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
QuantConnect/Lean#10199 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
QuantConnect/Lean#10198 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
QuantConnect/Lean#10197 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di QuantConnect/Lean
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
dotnet-outdated/dotnet-outdated#871 ·
I maintainer di solito rispondono entro 1 giorno
-
triage/untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
unoplatform/uno#26676 ·
I maintainer di solito rispondono entro 1 giorno
-
.NET bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
[问题] 导出文件时,提示后面还有1243390911步Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
jhaizhou-ops/S2Arm#2177 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
area-CodeGen-coreclr untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
dotnet/runtime#138181 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno