Skip to content

Commit

Permalink
log: fix crash on removing log directory
Browse files Browse the repository at this point in the history
`tt log -f` command crashes if log directory is removed.
  • Loading branch information
psergee committed Sep 24, 2024
1 parent 5e0e46b commit 8a04821
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Fixed

- `tt log -f` crash on removing log directory.

### Changed

## [2.4.0] - 2024-08-07
Expand Down
13 changes: 12 additions & 1 deletion cli/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"

"github.com/apex/log"
"github.com/fatih/color"
"github.com/nxadm/tail"
)
Expand Down Expand Up @@ -163,7 +164,17 @@ func Follow(ctx context.Context, out chan<- string, logFormatter LogFormatter, f
t.Stop()
t.Wait()
return
case line := <-t.Lines:
case line, more := <-t.Lines:
if !more {
err := t.Stop()
if err != nil {
log.Error(err.Error())
} else {
log.Errorf("The log file %q is unavailable for reading. Exiting.")
}
close(out)
return
}
out <- logFormatter(line.Text)
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/integration/log/test_log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import subprocess
import time

Expand Down Expand Up @@ -253,3 +254,25 @@ def test_log_output_default_follow_want_zero_last(tt_cmd, mock_env_dir):
assert 'app0:inst1' not in output
assert 'app0:inst2' not in output
assert 'app1:inst0' not in output


def test_log__dir_removed_after_follow(tt_cmd, mock_env_dir):
cmd = [tt_cmd, 'log', '-f']
process = subprocess.Popen(
cmd,
cwd=mock_env_dir,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
text=True,
)

wait_for_lines_in_output(process.stdout,
['app0:inst0: line 19', 'app1:inst2: line 19',
'app0:inst1: line 19', 'app1:inst1: line 19'])

var_dir = os.path.join(mock_env_dir, 'ie', 'app0', 'var')
assert os.path.exists(var_dir)
shutil.rmtree(var_dir)

assert process.wait(2) == 0
assert "Failed to detect creation of" in process.stdout.read()

0 comments on commit 8a04821

Please sign in to comment.