Skip to content

Commit 3003c89

Browse files
authored
Pending tasks timeout (#2332)
1 parent a02f6fd commit 3003c89

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

conf/default/cuckoo.conf.default

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ scaling_semaphore = off
3333
# A configurable wait time between updating the limit value of the scaling bounded semaphore
3434
scaling_semaphore_update_timer = 10
3535

36+
# Specify a timeout for tasks, useful if you are bound to timely reports awaited by users
37+
task_timeout = off
38+
task_pending_timeout = 0
39+
task_timeout_scan_interval = 30
40+
3641
# Enable creation of memory dump of the analysis machine before shutting
3742
# down. Even if turned off, this functionality can also be enabled at
3843
# submission. Currently available for: VirtualBox and libvirt modules (KVM).

lib/cuckoo/core/database.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@
169169
tasks_tags = Table(
170170
"tasks_tags",
171171
Base.metadata,
172-
Column("task_id", Integer, ForeignKey("tasks.id")),
173-
Column("tag_id", Integer, ForeignKey("tags.id")),
172+
Column("task_id", Integer, ForeignKey("tasks.id", ondelete='cascade')),
173+
Column("tag_id", Integer, ForeignKey("tags.id", ondelete='cascade')),
174174
)
175175

176176

@@ -268,7 +268,7 @@ class Guest(Base):
268268
manager = Column(String(255), nullable=False)
269269
started_on = Column(DateTime(timezone=False), default=datetime.now, nullable=False)
270270
shutdown_on = Column(DateTime(timezone=False), nullable=True)
271-
task_id = Column(Integer, ForeignKey("tasks.id"), nullable=False, unique=True)
271+
task_id = Column(Integer, ForeignKey("tasks.id", ondelete='cascade'), nullable=False, unique=True)
272272

273273
def __repr__(self):
274274
return f"<Guest({self.id}, '{self.name}')>"
@@ -2078,6 +2078,21 @@ def list_tasks(
20782078

20792079
return tasks
20802080

2081+
def check_tasks_timeout(self, timeout):
2082+
"""Find tasks which were added_on more than timeout ago and clean
2083+
"""
2084+
tasks: List[Task] = []
2085+
ids_to_delete = []
2086+
if timeout == 0:
2087+
return
2088+
search = self.session.query(Task).filter(Task.status == TASK_PENDING).order_by(Task.added_on.desc())
2089+
tasks = search.all()
2090+
for task in tasks:
2091+
if task.added_on + timedelta(seconds = timeout) < datetime.now():
2092+
ids_to_delete.append(task.id)
2093+
if len(ids_to_delete) > 0:
2094+
self.session.query(Task).filter(Task.id.in_(ids_to_delete)).delete(synchronize_session=False)
2095+
20812096
def minmax_tasks(self):
20822097
"""Find tasks minimum and maximum
20832098
@return: unix timestamps of minimum and maximum

lib/cuckoo/core/scheduler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def __init__(self, maxcount=0):
6464
self.analysis_threads: List[AnalysisManager] = []
6565
self.analyzing_categories, categories_need_VM = load_categories()
6666
self.machinery_manager = MachineryManager() if categories_need_VM else None
67+
if self.cfg.cuckoo.get("task_timeout", False):
68+
self.next_timeout_time = time.time() + self.cfg.cuckoo.get("task_timeout_scan_interval", 30)
6769
log.info("Creating scheduler with max_analysis_count=%s", self.max_analysis_count or "unlimited")
6870

6971
@property
@@ -98,6 +100,12 @@ def do_main_loop_work(self, error_queue: queue.Queue) -> SchedulerCycleDelay:
98100
if self.is_short_on_disk_space():
99101
return SchedulerCycleDelay.LOW_DISK_SPACE
100102

103+
if self.cfg.cuckoo.get("task_timeout", False):
104+
if self.next_timeout_time < time.time():
105+
self.next_timeout_time = time.time() + self.cfg.cuckoo.get("task_timeout_scan_interval", 30)
106+
with self.db.session.begin():
107+
self.db.check_tasks_timeout(self.cfg.cuckoo.get("task_pending_timeout", 0))
108+
101109
analysis_manager: Optional[AnalysisManager] = None
102110
with self.db.session.begin():
103111
max_machines_reached = False

tests/test_analysis_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def test_init(self, task: Task):
129129
"sanitize_to_len": 24,
130130
"scaling_semaphore": False,
131131
"scaling_semaphore_update_timer": 10,
132+
"task_pending_timeout": 0,
133+
"task_timeout": False,
134+
"task_timeout_scan_interval": 30,
132135
"freespace_processing": 15000,
133136
"periodic_log": False,
134137
"fail_unserviceable": True,

0 commit comments

Comments
 (0)