Skip to content

Commit

Permalink
fix: remove temp file on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Sep 19, 2024
1 parent fe07d68 commit 8c2f4b1
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (a *Anvil) Start(ctx context.Context) error {

if len(a.cfg.GenesisJSON) > 0 && a.cfg.ForkConfig == nil {
tempFile, err := os.CreateTemp("", "genesis-*.json")
defer a.removeTempFile(tempFile)

if err != nil {
return fmt.Errorf("error creating temporary genesis file: %w", err)
}
Expand Down Expand Up @@ -122,6 +124,8 @@ func (a *Anvil) Start(ctx context.Context) error {

// Handle stdout/stderr
logFile, err := os.CreateTemp("", fmt.Sprintf("anvil-chain-%d-", a.cfg.ChainID))
defer a.removeTempFile(logFile)

if err != nil {
return fmt.Errorf("failed to create temp log file: %w", err)
}
Expand Down Expand Up @@ -301,3 +305,9 @@ func (a *Anvil) SimulatedLogs(ctx context.Context, tx *types.Transaction) ([]typ

return logs, err
}

func (a *Anvil) removeTempFile(tempFile *os.File) {
if err := os.Remove(tempFile.Name()); err != nil {
a.log.Warn("failed to remove temp genesis file", "file.path", tempFile.Name(), "err", err)
}
}

0 comments on commit 8c2f4b1

Please sign in to comment.