Skip to content

[BUG] - Potential Memory Leak Due to Timer Creation in AfterFunc and selectExecJobsOutForRescheduling #777

@hayotbisonai

Description

@hayotbisonai

Describe the bug

I've noticed a potential memory issue related to the use of timers and goroutines in the AfterFunc implementation in the clockwork package and the job rescheduling process in the scheduler. When profiling the application, I observed significant memory consumption, particularly related to time.goFunc and goroutines created by AfterFunc.

Key points:

  • The AfterFunc call creates a new goroutine each time a timer is set, and this can lead to many goroutines being created and left hanging if they are not stopped or properly handled.
  • The frequent scheduling and rescheduling of jobs in selectExecJobsOutForRescheduling might contribute to the problem due to timer and goroutine buildup.

Profile Data:

Showing nodes accounting for 239.24MB, 76.13% of 314.25MB total
Dropped 126 nodes (cum <= 1.57MB)
Showing top 10 nodes out of 107
      flat  flat%   sum%        cum   cum%
   79.66MB 25.35% 25.35%    79.66MB 25.35%  time.goFunc
   31.43MB 10.00% 35.35%    31.43MB 10.00%  github.com/go-co-op/gocron/v2.(*scheduler).selectNewJob
   31.01MB  9.87% 45.22%    31.01MB  9.87%  runtime.malg
   21.50MB  6.84% 52.06%    34.30MB 10.92%  context.withCancel (inline)
   19.01MB  6.05% 58.11%    21.05MB  6.70%  github.com/go-co-op/gocron/v2.(*scheduler).selectExecJobsOutForRescheduling

As seen, time.goFunc and the scheduling-related functions are taking a substantial amount of memory and keeps growing

To Reproduce

Steps to reproduce the behavior:

Version

Expected behavior

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions