Skip to content

proxy: panic: unexpected end of JSON input #4478

Closed

Description

@ialidzhikov

Description

tl;dr: This issue is a reopen of #2374 with more insights. In #4293 we fixed the predominant root cause for the issue - the non-graceful shutdown. However, the issue can still occur and the proxy cannot recover automatically from it leaking (losing track of) all the cached images in its storage.


We observed a new occurrence of #2374. Logs of the proxy

time="2024-10-04T06:21:24.616587724Z" level=info msg="redis not configured" go.version=go1.22.4 instance.id=305395bb-a130-4af9-9f1e-c3240f6769ea service=registry version=3.0.0-beta.1
time="2024-10-04T06:21:24.616627486Z" level=info msg="Starting upload purge in 58m0s" go.version=go1.22.4 instance.id=305395bb-a130-4af9-9f1e-c3240f6769ea service=registry version=3.0.0-beta.1
panic: unexpected end of JSON input

goroutine 1 [running]:
github.com/distribution/distribution/v3/registry/handlers.NewApp({0x1c03e20, 0xc0000c3e60}, 0xc0008ea488)
	github.com/distribution/distribution/v3/registry/handlers/app.go:353 +0x1cc5
github.com/distribution/distribution/v3/registry.NewRegistry({0x1c03e20?, 0xc0000c2bd0?}, 0xc0008ea488)
	github.com/distribution/distribution/v3/registry/registry.go:142 +0x56
github.com/distribution/distribution/v3/registry.init.func1(0x2734b00, {0xc0005919e0, 0x1, 0x1})
	github.com/distribution/distribution/v3/registry/registry.go:111 +0x10a
github.com/spf13/cobra.(*Command).execute(0x2734b00, {0xc0005919a0, 0x1, 0x1})
	github.com/spf13/[email protected]/command.go:987 +0xab1
github.com/spf13/cobra.(*Command).ExecuteC(0x2734de0)
	github.com/spf13/[email protected]/command.go:1115 +0x3ff
github.com/spf13/cobra.(*Command).Execute(0xc0000061c0?)
	github.com/spf13/[email protected]/command.go:1039 +0x13
main.main()
	github.com/distribution/distribution/v3/cmd/registry/main.go:26 +0x1a

The logs from the instance before this panic occurs:

# ...
2024-10-03 15:43:54	time="2024-10-03T15:43:54.749227425Z" level=info msg="stopping server gracefully. Draining connections for 25s" go.version=go1.22.4 instance.id=5731de6f-05af-43a1-b44b-a3c1bf25e067 service=registry version=3.0.0-beta.1
2024-10-03 15:43:54	time="2024-10-03T15:43:54.750578013Z" level=fatal msg="error writing scheduler state: filesystem: write /var/lib/registry/scheduler-state.json: no space left on device"

As you can see from the logs, the disk space was exhausted.

When I checked, the /var/lib/registry/scheduler-state.json file was empty:

$ ls -la /var/lib/registry/scheduler-state.json
-rw-r--r-- 1 root root 0 Oct  3 15:43 /var/lib/registry/scheduler-state.json

$ cat /var/lib/registry/scheduler-state.json

This is a pretty bad state. Because the volume of the proxy is full with cached images. But the scheduler-state.json is empty and the proxy lost track of all these blobs. Hence, it won't be able to delete the blobs when the TTL expires.

The reason for the scheduler-state.json file to be empty seems to be the specific logic we have for writing a file:

func (d *driver) Writer(ctx context.Context, subPath string, append bool) (storagedriver.FileWriter, error) {
fullPath := d.fullPath(subPath)
parentDir := path.Dir(fullPath)
if err := os.MkdirAll(parentDir, 0o777); err != nil {
return nil, err
}
fp, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return nil, err
}
var offset int64
if !append {
err := fp.Truncate(0)
if err != nil {
fp.Close()
return nil, err
}
} else {
n, err := fp.Seek(0, io.SeekEnd)
if err != nil {
fp.Close()
return nil, err
}
offset = n
}
return newFileWriter(fp, offset), nil
}

We first truncate the file, then we write the new content. If we cannot write the new content, then the file is only truncated. Any new start of the proxy fails with panic: unexpected end of JSON input because the truncated file does not contain a valid JSON document.

It should be possible to improve the logic for writing the file and prevent such type of issues. For example first writing the content to a new file and moving the new file to the place of the old one.

Reproduce

  1. Setup a proxy
  2. Make sure that there is no available disk space
  3. Stop the proxy process so that it tries to write to the scheduler-state.json file
  4. Make sure that it cannot write to the scheduler-state.json file and leaves it in truncated state.
  5. Make sure that any new start of the proxy fails with panic: unexpected end of JSON input

Expected behavior

proxy shouldn't leave the scheduler-state.json file truncated when it cannot write to it due to no available disk space.

registry version

3.0.0-beta.1

Additional Info

No response

Activity

  1. added 4 commits that reference this issue on Apr 16, 2025
  2. added a commit that references this issue on May 19, 2025
  3. LiuShuaiyi commented on Jun 13, 2025

    @LiuShuaiyi

    @onporat Thanks for sending the PR to fix this issue. I have 2 small questions about the pull request: https://github.com/distribution/distribution/pull/4624/files#r2146077767. PTAL.

  4. ialidzhikov commented on Jul 28, 2025

    @ialidzhikov
    ContributorAuthor

    This issue is hopefully fixed with #4624.
    @milosgajdos , is it possible to cut a new release so that we can adopt the bugfix?

  5. milosgajdos commented on Aug 5, 2025

    @milosgajdos
    Member

    I need to go through PR backlog first, but yeah I'd love to cut a release soon!

  6. ialidzhikov commented on Sep 3, 2025

    @ialidzhikov
    ContributorAuthor

    @milosgajdos , any news? We are looking forward to adopt a new release to adopt the fix for this issue.

  7. ialidzhikov commented on Oct 6, 2025

    @ialidzhikov
    ContributorAuthor

    Hi @milosgajdos ,

    Is there any progress with a new patch release for the Distribution project?

  8. milosgajdos commented on Oct 17, 2025

    @milosgajdos
    Member

    I'd like to but $DAY_JOB has kept me busy and free work for CNCF has low priority

  9. ningmingxiao commented on Feb 1, 2026

    @ningmingxiao
    Contributor

    pr #4774 may fix the data is empty

  10. ialidzhikov commented on Apr 21, 2026

    @ialidzhikov
    ContributorAuthor

    I am closing this issue as it appears to be fixed in 3.1.0 with #4624.
    I will reopen if we observe it again with 3.1.0.

  11. kastl-ars commented on Apr 29, 2026

    @kastl-ars

    Hi everyone, I just saw my registry hitting this error. I upgraded to 3.1.0 but still see this. Should upgrading fix the issue, i.e. repair the underlying problems, or do I need to manually clean up? Or is it simply not properly fixed?

    time="2026-04-29T05:31:00.840816971Z" level=info msg="redis not configured" go.version=go1.25.8 instance.id=019dd7b8-1d67-7c77-9ddb-8ee50458ec5f service=registry version=3.1.0
    time="2026-04-29T05:31:00.840965696Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.25.8 instance.id=019dd7b8-1d67-7c77-9ddb-8ee50458ec5f service=registry version=3.1.0
    time="2026-04-29T05:31:00.840886621Z" level=info msg="Starting upload purge in 48m0s" go.version=go1.25.8 instance.id=019dd7b8-1d67-7c77-9ddb-8ee50458ec5f service=registry version=3.1.0
    panic: unexpected end of JSON input
    
    goroutine 1 [running]:
    github.com/distribution/distribution/v3/registry/handlers.NewApp({0x2495aa0, 0xc000564810}, 0xc0006a1908)
            github.com/distribution/distribution/v3/registry/handlers/app.go:353 +0x1d05
    github.com/distribution/distribution/v3/registry.NewRegistry({0x2495aa0?, 0xc000705710?}, 0xc0006a1908)
            github.com/distribution/distribution/v3/registry/registry.go:150 +0x56
    github.com/distribution/distribution/v3/registry.init.func1(0x36e6640, {0xc00050ead0, 0x1, 0x1})
            github.com/distribution/distribution/v3/registry/registry.go:119 +0x106
    github.com/spf13/cobra.(*Command).execute(0x36e6640, {0xc00050ea90, 0x1, 0x1})
            github.com/spf13/[email protected]/command.go:1019 +0xae7
    github.com/spf13/cobra.(*Command).ExecuteC(0x36e6920)
            github.com/spf13/[email protected]/command.go:1148 +0x465
    github.com/spf13/cobra.(*Command).Execute(0xc000002540?)
            github.com/spf13/[email protected]/command.go:1071 +0x13
    main.main()
            github.com/distribution/distribution/v3/cmd/registry/main.go:26 +0x1a
    
    
  12. milosgajdos commented on Apr 29, 2026

    @milosgajdos
    Member
  13. kastl-ars commented on Apr 30, 2026

    @kastl-ars

    #4860

    Care to elaborate? Seems like this is not released yet, any ETA on a new release?

  14. milosgajdos commented on Apr 30, 2026

    @milosgajdos
    Member

    we'll do a patch release soon; no ETA, you can pull the :latest tag from distirbution namespace or GH

  15. kastl-ars commented on May 1, 2026

    @kastl-ars

    we'll do a patch release soon; no ETA, you can pull the :latest tag from distirbution namespace or GH

    Just tested with the 3.1.1 release on ghcr, no change, still panics...

  16. milosgajdos commented on May 1, 2026

    @milosgajdos
    Member

    what storage driver are you? We need more details here as all of them bring their own idiosyncrasies

    I'm reopening this issue

  17. ialidzhikov commented on May 1, 2026

    @ialidzhikov
    ContributorAuthor

    I don't know how #4860 is related.


    Hi everyone, I just saw my registry hitting this error. I upgraded to 3.1.0 but still see this. Should upgrading fix the issue, i.e. repair the underlying problems, or do I need to manually clean up? Or is it simply not properly fixed?

    @kastl-ars , you have to clean up the already corrupted scheduler-state.json. The fix in #4624 only prevents new occurrences of the issue. It does not recover from already broken (empty) scheduler-state.json file.

    tl;dr: @kastl-ars I think the issue is fixed. Your confusion comes from your expectation that the fix will also recover the broken (empty) scheduler-state.json file. Just fix manually the scheduler-state.json file (for example delete it and delete the cached content) and you should not hit the same issue again.


    @dimitar-kostadinov was testing 3.1.0 version on our side. He was able to confirm that this issue is fixed with #4624.

    But he found other issues with the scenario when the proxy's volume disk space gets exhausted.

    He found that image pulls from the proxy fail.
    kubelet events:

    Warning  Failed     4m54s                   kubelet            spec.containers{t3}: Failed to pull image "nginx:1.23.0": rpc error: code = FailedPrecondition desc = failed to pull and unpack image "docker.io/library/nginx:1.23.0": failed commit on ref "layer-sha256:a96aaf9a9ec3745f5d98823cab45f1a79188b278e9d181203346dcf458fe13f0": unexpected commit digest sha256:4242180ee6c12a33c4d1d87cba0cc5ec4cfe7edb12d596a8ab66b4142ec3e923, expected sha256:a96aaf9a9ec3745f5d98823cab45f1a79188b278e9d181203346dcf458fe13f0: failed precondition
    

    proxy log:

    time="2026-04-17T13:23:16.556160604Z" level=error msg="response completed with error" err.code=unknown err.detail="write /var/lib/registry/docker/registry/v2/repositories/library/nginx/_uploads/019d9b9c-2a69-779e-b5e4-1decc64db585/data: no space left on device" err.message="unknown error" go.version=go1.25.8 http.request.host="100.80.104.19:5000" http.request.id=019d9b9c-298b-7c45-ad1d-71b414cba0bb http.request.method=GET http.request.remoteaddr="10.250.0.4:54624" http.request.uri="/v2/library/nginx/blobs/sha256:461246efe0a75316d99afdbf348f7063b57b0caeee8daab775f1f08152ea36f4?ns=docker.io" http.request.useragent=containerd/2.1.5 http.response.contenttype=application/json http.response.duration=576.352379ms http.response.status=500 http.response.written=3260631 instance.id=019d9b7d-0e0d-71d1-aa9d-6aa184f5eb6b service=registry vars.digest="sha256:461246efe0a75316d99afdbf348f7063b57b0caeee8daab775f1f08152ea36f4" vars.name=library/nginx version=3.1.0
    

    Most likely he will open a new issue with these findings but we wanted to gain more insights.

  18. milosgajdos commented on May 1, 2026

    @milosgajdos
    Member

    Yeah I mistakenly attributed wrong PR

  19. kastl-ars commented on May 1, 2026

    @kastl-ars

    @kastl-ars , you have to clean up the already corrupted scheduler-state.json. The fix in #4624 only prevents new occurrences of the issue. It does not recover from already broken (empty) scheduler-state.json file.

    Thanks, that was my guess, hence my question a little up in the comments. Will try to clean this up (need to find a way to access the PV with the pod immediately crashing, but I'll find a way)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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