nodejs / nodejs/node

perf_hooks.performance.timerify() with histogram: RangeError [ERR_OUT_OF_RANGE] on arm64

未关闭
#41,641 5 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

perf_hooks
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v14, v16, v17, v18

Platform

macOS v12.4 (21F79).

Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64

Subsystem

perf_hooks

What steps will reproduce the bug?

Run this snippet:

import {createHistogram, performance} from 'node:perf_hooks';

const fn = () => {};

const timerified = performance.timerify(fn, {histogram: createHistogram()});

for (let i = 0; i < 100_000; i++) {
	try {
		timerified();
	} catch (error) {
		console.error('Crashed on iteration', i);

		throw error;
	}
}
How often does it reproduce? Is there a required condition?

100% consistent reproduction, requires arm64, maybe requires macOS.

What is the expected behavior?

Calling the timerified function returned from perf_hooks.performance.timerify() should successfully record to the histogram.

What do you see instead?
node:internal/errors:466
    ErrorCaptureStackTrace(err);
    ^

RangeError [ERR_OUT_OF_RANGE]: The value of "val" is out of range. It must be a safe integer greater than 0. Received 0
    at new NodeError (node:internal/errors:377:5)
    at RecordableHistogram.record (node:internal/histogram:290:13)
    at processComplete (node:internal/perf/timerify:40:15)
    at timerified fn (node:internal/perf/timerify:88:5)
    at file:///Users/jonah/programming/benchmark/index.mjs:9:3
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) {
  code: 'ERR_OUT_OF_RANGE'
}

Node.js v18.1.0
Additional information

Originally reported here: https://github.com/sindresorhus/yoctocolors/pull/9#issuecomment-1019033882


The bug appears to be from the implementation of perf_hooks.performance.timerify() with a histogram, when calling the returned timerified function. The error comes from the input validation of RecordableHistogram#record(), attempting to record a value x < 1 will throw. For whatever reason timerify() seems to be recording an execution duration of 0 and trying to record that. See the error stacktrace for more info.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先在 arm64 上复现该 snippet,然后检查 internal/perf/timerify 和 internal/histogram,尤其是 stack trace 中显示的 processComplete 和 RecordableHistogram.record。完成的标准是 timerify() 记录执行过程且不抛出 ERR_OUT_OF_RANGE,并且该行为由适当的回归测试覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
backend, performance
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。