Skip to content

Commit

Permalink
fix(player): Prevent slight memory leak by ensuring player is always …
Browse files Browse the repository at this point in the history
…closed
  • Loading branch information
gabe565 committed Jul 28, 2024
1 parent 83b3108 commit b4dbf1e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
5 changes: 0 additions & 5 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ linters-settings:
gosec:
excludes:
- G306
forbidigo:
forbid:
- "^(fmt\\.Print(|f|ln)|print|println)$"
- p: "^tea\\.Quit$"
msg: tea.Quit may cause a memory leak

linters:
enable:
Expand Down
6 changes: 4 additions & 2 deletions cmd/play/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func run(cmd *cobra.Command, args []string) error {
return err
}

program := tea.NewProgram(
player.NewPlayer(&m, log.Level(zerolog.ErrorLevel), nil),
p := player.NewPlayer(&m, log.Level(zerolog.ErrorLevel), nil)
defer p.Close()

program := tea.NewProgram(p,
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
Expand Down
6 changes: 0 additions & 6 deletions internal/player/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ func tick(ctx context.Context, d time.Duration, msg tea.Msg) tea.Cmd {
}
}
}

type quitMsg struct{}

func Quit() tea.Msg {
return quitMsg{}
}
25 changes: 13 additions & 12 deletions internal/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (p *Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case p.speed >= 0:
frameDiff = 1
if p.frame+frameDiff >= len(p.movie.Frames) {
return p, Quit
return p, tea.Quit
}
case p.frame <= 0:
p.speed = 1
Expand All @@ -93,7 +93,7 @@ func (p *Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
duration := p.movie.Frames[p.frame].CalcDuration(speed)
for duration < time.Second/15 {
if p.frame+frameDiff >= len(p.movie.Frames) {
return p, Quit
return p, tea.Quit
} else if p.frame+frameDiff <= 0 {
p.speed = 1
p.activeOption = 4
Expand All @@ -108,7 +108,7 @@ func (p *Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
p.optionViewStale = true
switch {
case key.Matches(msg, p.keymap.quit):
return p, Quit
return p, tea.Quit
case key.Matches(msg, p.keymap.left):
if p.selectedOption > 0 {
p.selectedOption--
Expand Down Expand Up @@ -164,15 +164,6 @@ func (p *Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
}
case quitMsg:
if p.frame >= len(p.movie.Frames)-1 {
p.log.Info().Msg("Finished movie")
} else {
p.log.Info().Msg("Disconnected early")
}
p.clearTimeouts()
p.zone.Close()
return p, tea.Quit //nolint:forbidigo
case Option:
p.optionViewStale = true
switch msg {
Expand Down Expand Up @@ -352,3 +343,13 @@ func (p *Player) clearTimeouts() {
p.playCancel()
}
}

func (p *Player) Close() {
if p.frame >= len(p.movie.Frames)-1 {
p.log.Info().Msg("Finished movie")
} else {
p.log.Info().Msg("Disconnected early")
}
p.clearTimeouts()
p.zone.Close()
}
8 changes: 6 additions & 2 deletions internal/server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ func (s *SSHServer) Handler(m *movie.Movie) bubbletea.Handler {
}
}

player := player.NewPlayer(m, logger, renderer)
return player, []tea.ProgramOption{
p := player.NewPlayer(m, logger, renderer)
go func() {
<-session.Context().Done()
p.Close()
}()
return p, []tea.ProgramOption{
tea.WithFPS(30),
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
Expand Down
4 changes: 3 additions & 1 deletion internal/server/telnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi
}

p := player.NewPlayer(m, logger, telnet.MakeRenderer(outW, profile))
defer p.Close()

opts := []tea.ProgramOption{
tea.WithInput(inR),
tea.WithOutput(outW),
Expand All @@ -157,7 +159,7 @@ func (s *TelnetServer) Handler(ctx context.Context, conn net.Conn, m *movie.Movi
})
}
case <-ctx.Done():
program.Send(player.Quit())
program.Send(tea.Quit())
return
}
}
Expand Down

0 comments on commit b4dbf1e

Please sign in to comment.