Skip to content

Commit 69b4cc8

Browse files
authored
fix: loki.source.file drain tasks on exit (#4593)
* Drain on exit
1 parent 5fc4129 commit 69b4cc8

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Main (unreleased)
5353

5454
- Fix an issue where component shutdown could block indefinitely by adding a warning log message and a deadline of 10 minutes. The deadline can be configured with the `--feature.component-shutdown-deadline` flag if the default is not suitable. (@thampiotr)
5555

56+
- Fix potential deadlocks in `loki.source.file` and `loki.source.journal` when component is shutting down. (@kalleep, @thampiotr)
57+
5658
v1.11.1
5759
-----------------
5860

internal/component/loki/source/file/file.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ func (c *Component) Run(ctx context.Context) error {
145145
})
146146
defer func() {
147147
level.Info(c.opts.Logger).Log("msg", "loki.source.file component shutting down, stopping readers and positions file")
148+
149+
// Start black hole drain routine to prevent deadlock when we call c.t.Stop().
150+
drainCtx, cancelDrain := context.WithCancel(context.Background())
151+
defer cancelDrain()
152+
go func() {
153+
for {
154+
select {
155+
case <-drainCtx.Done():
156+
return
157+
case <-c.handler.Chan(): // Ignore the remaining entries
158+
}
159+
}
160+
}()
161+
148162
c.tasksMut.RLock()
149163
c.stopping.Store(true)
150164
runner.Stop()

0 commit comments

Comments
 (0)