Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing JSON parsing in Node 22 #3328

Closed
stefan-garofalo opened this issue Jun 14, 2024 · 13 comments · Fixed by #3338
Closed

Failing JSON parsing in Node 22 #3328

stefan-garofalo opened this issue Jun 14, 2024 · 13 comments · Fixed by #3338
Labels

Comments

@stefan-garofalo
Copy link

stefan-garofalo commented Jun 14, 2024

EDIT
I managed to reproduce the error locally and changed the bug description, and reproduction steps accordingly

Bug Description

When making a fetch call the JSON parsing fails and spits out some broken chars. This only happens in Node 22.3.0, does not happen in node 22.1.0

Reproducible By

Fetching data from an API (in my case Wordpress) and parsing the body as json. Currently this happens in a Next.js project. When reaching my homepage I make a call to my cms

nvm use 22.3.0
async function call(url) {
  const response = await fetch(`${WP_DEFAULT_URL}${url}`, {});
  return response.json();
}

Expected Behavior

The JSON being parsed correctly. This behavior came up circa 3 days ago, I figure it has something to do with the latest Node 22.3.0 release

Logs & Screenshots

I added a trycatch to log the query if failing to see more clearly in the github logs, since I firstly bumped into the problem during a github action build

#9 28.25 SyntaxError: Unexpected non-whitespace character after JSON at position 2908 (line 1 column 2909)
#9 28.25     at JSON.parse (<anonymous>)
#9 28.25     at parseJSONFromBytes (node:internal/deps/undici/undici:5477:19)
#9 28.25     at successSteps (node:internal/deps/undici/undici:5459:27)
#9 28.25     at fullyReadBody (node:internal/deps/undici/undici:4378:9)
#9 28.25     at async consumeBody (node:internal/deps/undici/undici:5468:7)
#9 28.25     at async Object.getItem (/usr/src/app/.next/server/app/cerca/page.js:1:8661)
#9 28.25     at async o (/usr/src/app/.next/server/app/[lang]/(pages)/cerca/page.js:1:4250)
{... some other json from my cms...},
[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}]����!*�� �!*��ȉ!*��P�!*��x�!*����!*����.(�!*��@�!*���~!*�����y��@�!*����b������!*����!*�������L��c�*����c�*���`��*�����*���^����!*����!*���c�� �!*�� �!*����Ȋ!*��(�)*�������c�*����c�*���(�)*��� d�*��������L�
#9 28.25 ��
�
#9 28.25 ,����y�����y���������L�
#9 28.25 ��
�
#9 28.25 ,��
#9 28.25 D������t�0	�
#9 28.25 p���y�����y����g%8��y��8��y������3�����L�
#9 28.25 ��
�
#9 28.25 ,��
#9 28.25 D������t�0	�
#9 28.25 p� 	�H�8	����������
#9 28.25 ��������8��y��8��y����!*��?��������������������X��y��X��y���\�c�*����c�*���(�)*��� d�*���xd�*����d�*���xe�*����e�*���`��*�����*��0��*�����*����Ȋ!*���d�*��T�������L�
#9 28.25 ��
�
#9 28.25 ,��
#9 28.25 D������t�0	�
#9 28.25 p� 	�H�8	����������
#9 28.25 ��������H�0	�
#9 28.25 �����$�������%��
���������������	������� 
#9 28.25 ��!

Environment

Github worker using the latest Node 22.3.0 Docker image, but it also happens locally using the latest Node 22.3.0

Additional context

My application code did not change in such a timeframe, nor its configs. I managed to work around the issue and correctly publish my repository's images by downgrading to Node 20.

Since the stack trace refers to files inside /undici I figured issuing in this repository would have fit better rather than Node itself

@stefan-garofalo stefan-garofalo added the bug Something isn't working label Jun 14, 2024
@ronag
Copy link
Member

ronag commented Jun 14, 2024

@KhafraDev Seems valid.

@mcollina
Copy link
Member

@stefan-garofalo can you try to isolate what combination of response body + headers is causing this? In order to fix we need a server that reproduces the problem, otherwise it's impossible to find out.

Nothing really stands up by looking at the diffs.

@stefan-garofalo
Copy link
Author

@stefan-garofalo can you try to isolate what combination of response body + headers is causing this? In order to fix we need a server that reproduces the problem, otherwise it's impossible to find out.

Nothing really stands up by looking at the diffs.

I managed to specifically install the latest node 22 version (22.3.0) locally and now I am actually reproducing the error outside of github workers too.

This is the response:

Response {
  status: 200,
  statusText: '',
  headers: Headers {
    'access-control-allow-headers': 'Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type',
    'access-control-expose-headers': 'X-WP-Total, X-WP-TotalPages, Link',
    allow: 'GET',
    'content-type': 'application/json; charset=UTF-8',
    date: 'Fri, 14 Jun 2024 09:30:02 GMT',
    link: '<MY_CMS_URL>; rel="https://api.w.org/"',
    server: 'Apache/2.4.59 (Debian)',
    vary: 'Origin',
    'x-content-type-options': 'nosniff',
    'x-powered-by': 'PHP/8.2.19',
    'x-robots-tag': 'noindex',
    'x-wp-total': '1',
    'x-wp-totalpages': '1',
    'transfer-encoding': 'chunked'
  },
  body: ReadableStream { locked: false, state: 'readable', supportsBYOB: true },
  bodyUsed: false,
  ok: true,
  redirected: false,
  type: 'default',
  url: 'MY_CMS_URL'
}

And here follows the body

[
  {
    "id": 13,
    "date": "2024-05-29T11:49:34",
    "date_gmt": "2024-05-29T09:49:34",
    "guid": {
      "rendered": "MY_CMS_URL"
    },
    "modified": "2024-06-13T17:35:54",
    "modified_gmt": "2024-06-13T15:35:54",
    "slug": "homepage",
    "status": "publish",
    "type": "page",
    "link": "MY_CMS_URL",
    "title": {
      "rendered": "Homepage"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "excerpt": {
      "rendered": "",
      "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "parent": 0,
    "menu_order": 4,
    "comment_status": "closed",
    "ping_status": "closed",
    "template": "",
    "meta": {
      "footnotes": ""
    },
    "acf": {
      "hero": {
        "titolo": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR SED DIAM",
        "paragrafo": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
        "cta": {
          "etichetta": "lorem",
          "link": "/"
        }
      },
      "pagine": {
        "lista": [49, 67]
      },
      "storie": {
        "titolo": "Lorem",
        "lista": [
          {
            "titolo": "Lorem ipsum dolor sit amet consetetur",
            "cover": {
              "ID": 44,
              "id": 44,
              "status": "inherit",
              "uploaded_to": 13,
              "date": "2024-05-30 15:24:42",
              "modified": "2024-05-30 15:24:48",
              "menu_order": 0,
              "mime_type": "image/png",
              "type": "image",
              "subtype": "png",

              "width": 1830,
              "height": 1866,
              "sizes": {
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium-width": 294,
                "medium-height": 300,
                "medium_large-width": 768,
                "medium_large-height": 783,
                "large-width": 1004,
                "large-height": 1024,
                "1536x1536-width": 1506,
                "1536x1536-height": 1536,
                "2048x2048-width": 1830,
                "2048x2048-height": 1866
              }
            },
            "colore": "light"
          },
          {
            "titolo": "Lorem ipsum dolor sit amet consetetur",
            "cover": {
              "ID": 44,
              "id": 44,
              "filesize": 89931,
              "author": "1",
              "uploaded_to": 13,
              "date": "2024-05-30 15:24:42",
              "modified": "2024-05-30 15:24:48",
              "menu_order": 0,
              "mime_type": "image/png",
              "type": "image",
              "subtype": "png",
              "width": 1830,
              "height": 1866,
              "sizes": {
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium-width": 294,
                "medium-height": 300,
                "medium_large-width": 768,
                "medium_large-height": 783,
                "large-width": 1004,
                "large-height": 1024,
                "1536x1536-width": 1506,
                "1536x1536-height": 1536,
                "2048x2048-width": 1830,
                "2048x2048-height": 1866
              }
            },
            "colore": "white"
          },
          {
            "titolo": "Lorem ipsum dolor sit amet consetetur",
            "cover": {
              "ID": 44,
              "id": 44,
              "filesize": 89931,
              "author": "1",
              "status": "inherit",
              "uploaded_to": 13,
              "date": "2024-05-30 15:24:42",
              "modified": "2024-05-30 15:24:48",
              "menu_order": 0,
              "mime_type": "image/png",
              "type": "image",
              "subtype": "png",
              "width": 1830,
              "height": 1866,
              "sizes": {
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium-width": 294,
                "medium-height": 300,
                "medium_large-width": 768,
                "medium_large-height": 783,
                "large-width": 1004,
                "large-height": 1024,
                "1536x1536-width": 1506,
                "1536x1536-height": 1536,
                "2048x2048-width": 1830,
                "2048x2048-height": 1866
              }
            },
            "colore": "secondary"
          },
          {
            "titolo": "Lorem ipsum dolor sit amet consetetur",
            "cover": {
              "ID": 44,
              "id": 44,
              "filesize": 89931,
              "author": "1",
              "status": "inherit",
              "uploaded_to": 13,
              "date": "2024-05-30 15:24:42",
              "modified": "2024-05-30 15:24:48",
              "menu_order": 0,
              "mime_type": "image/png",
              "type": "image",
              "subtype": "png",
              "width": 1830,
              "height": 1866,
              "sizes": {
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium-width": 294,
                "medium-height": 300,
                "medium_large": "MY_CMS_URL/wp-content/uploads/2024/05/logobuzzi-768x783.png",
                "medium_large-width": 768,
                "medium_large-height": 783,
                "large-width": 1004,
                "large-height": 1024,
                "1536x1536-width": 1506,
                "1536x1536-height": 1536,
                "2048x2048-width": 1830,
                "2048x2048-height": 1866
              }
            },
            "colore": "light"
          }
        ]
      },
      "buca": {
        "titolo": "LOREM IPSUM DOLOR SIT AMET",
        "paragrafo": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum."
      },
      "seo": {
        "title": "Homepage",
        "description": "Lorem ipsum dolor homepage",
        "image": false
      }
    },
    "_links": {
      "self": [
        {
          "href": "MY_CMS_URL"
        }
      ],
      "collection": [
        {
          "href": "MY_CMS_URL"
        }
      ],
      "about": [
        {
          "href": "MY_CMS_URL"
        }
      ],
      "author": [
        {
          "embeddable": true,
          "href": "MY_CMS_URL"
        }
      ],
      "replies": [
        {
          "embeddable": true,
          "href": "MY_CMS_URL"
        }
      ],
      "version-history": [
        {
          "count": 9,
          "href": "MY_CMS_URL"
        }
      ],
      "predecessor-version": [
        {
          "id": 204,
          "href": "MY_CMS_URL"
        }
      ],
      "wp:attachment": [
        {
          "href": "MY_CMS_URL"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  }
]

awaiting response.text() does not trigger any error, only json parsing. Parsing the response.text() using any online parser still works indeed

@mcollina
Copy link
Member

@stefan-garofalo can you include a complete example on reproducing it outside of GitHub Actions? Ideally client+server.

@stefan-garofalo
Copy link
Author

stefan-garofalo commented Jun 14, 2024

@mcollina I tried to reproduce it. When asked to provide headers and body as you requested I did nvm use 22.3.0 and could reproduce it on my project on my machine. I updated the issue's description accordingly

@stefan-garofalo
Copy link
Author

But I also failed to reproduce the issue outside of my current nextjs project. I tried node only with npm init, I tried a new empty Nextjs app, but still not reproducible

@mcollina
Copy link
Member

Keep going at it, without the reproduction will be impossible to fix :(.

  1. Is the new project talking to the same API of the old one?
  2. Are the versions of Next.js the same?
  3. Inside the "faulty" app, can you try installing undici directly and calling require('undici').fetch()?

@stefan-garofalo
Copy link
Author

  1. In the new project I tried calling both the API used in the faulty example and the public pokemon API
  2. Nextjs versions are the same
  3. I tried calling undici.fetch() directly and it now works in the faulty scenario.

So I guess its not really about undici. Maybe some conflicts between fetch ad Nextjs patches? Should I reopen this issue in Nextjs repository?

@mcollina
Copy link
Member

Possibly, we can investigate if we have a reproduction.

@KasperiP
Copy link

I encountered similar issues: 22.3-alpine failed, but after changing to 22.1-alpine, it worked fine.
https://github.com/luuppiry/luuppi-next/actions/runs/9527152450/job/26263513613
https://github.com/luuppiry/luuppi-next/actions/runs/9527336924/job/26263847778

@mcollina
Copy link
Member

We need a minimal reproduction to be able to fix.

@KhafraDev
Copy link
Member

afaik nextjs patches fetch which likely causes the issue. Without more information it looks like they're not decompressing the body.

@KhafraDev
Copy link
Member

KhafraDev commented Jun 17, 2024

The only PR that touched body was f979ec3, which would have landed in node v22 (undici v6.14.0). We can revert it to be safe, but without a reproduction... cc @tsctx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants