Skip to content

"file already closed" error when reading key from file #471

@imiric

Description

@imiric

Version 5.2.3 introduced a regression that causes "file already closed" errors when reading key files. This affects both regular files and process substitution. The issue is also present on the latest v5.3.0.

Steps to Reproduce

# works with v5.2.2
$ echo '{"user":"admin"}' | jwt -key priv.key -alg ES512 -sign -
eyJhbGciOiJFUzUx...
# fails with v5.2.3
$ echo '{"user":"admin"}' | jwt -key priv.key -alg ES512 -sign -
Error: couldn't read key: read priv.key: file already closed

# Same with process substitution
# works with v5.2.2
$ echo '{"user":"admin"}' | jwt -key <(cat priv.key) -alg ES512 -sign -
eyJhbGciOiJFUzUx...
# fails with v5.2.3
$ echo '{"user":"admin"}' | jwt -key <(cat priv.key) -alg ES512 -sign -
Error: couldn't read key: read /proc/self/fd/13: file already closed

Expected Behavior

The command should successfully read the key file and output the JWT.

Actual Behavior

Error: couldn't read key: read priv.key: file already closed

Or with process substitution:

Error: couldn't read key: read /proc/self/fd/13: file already closed

Environment

  • jwt: v5.2.3
  • Go: 1.24.4 linux/amd64
  • OS: Void Linux
  • Shell: bash 5.2.32

Root Cause

The issue appears to be caused by 12384ea#diff-fd1ad9799c0a3eec351605e9d9411bcdd7da014a2e5da1e95209a6f5d49adb97L94-R96 where defer f.Close() was replaced with immediate f.Close(), causing the file to be closed before the read operation completes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions