[BUG] Iterating a returned list of Protobuf messages throws outside the evaluation context

オープン
#367 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
javascript, nodejs, typescript
領域
compilers

調査の方向性

Start with the repro: run node repro.mjs and confirm the error is thrown at [...result], not during evaluation. The payload names ArrayList.values(), toCel(), getMsgDesc(), getEvalContext() and withEvalContext() as the path where the context is read after it has been cleared, so trace those. Done means the list from repositories iterates and prints 1 without the map(x, x) workaround, with the existing tests still passing.

索引モデルが issue の本文から書いたものです。

説明

Description

Iterating an evaluation result containing Protobuf messages throws cannot use getEvalContext outside of an evaluation, even when the message schema is registered in the CEL environment.

This reproduces with @bufbuild/cel 0.6.2 using only public APIs and a standard Protobuf message, without application-specific code. The evaluation itself returns a list; the exception is thrown when the caller subsequently iterates it.

Steps to Reproduce

In an empty directory, install:

npm init -y
npm install --save-exact @bufbuild/[email protected] @bufbuild/[email protected]
Failing case

Save the following as repro.mjs, then run node repro.mjs. The exception is thrown at [...result], after evaluation has returned.

import { celEnv, plan, parse, isCelError } from '@bufbuild/cel';
import { create, createRegistry } from '@bufbuild/protobuf';
import { FileDescriptorProtoSchema } from '@bufbuild/protobuf/wkt';

const env = celEnv({ registry: createRegistry(FileDescriptorProtoSchema) });
const messages = [create(FileDescriptorProtoSchema, { name: 'example.proto' })];

const result = plan(env, parse('repositories'))({ repositories: messages });
if (isCelError(result)) throw result;

const values = [...result];
console.log(values.length);

Expected Behavior

The returned list can be iterated after evaluation, and the program prints 1.

Actual Behavior

Evaluation returns a list, but iterating it throws:

Error: cannot use `getEvalContext` outside of an evaluation
Passing case: identity-map workaround

Save this independent example as workaround.mjs and run node workaround.mjs. The only difference is the CEL expression: repositories.map(x, x) instead of repositories.

import { celEnv, plan, parse, isCelError } from '@bufbuild/cel';
import { create, createRegistry } from '@bufbuild/protobuf';
import { FileDescriptorProtoSchema } from '@bufbuild/protobuf/wkt';

const env = celEnv({ registry: createRegistry(FileDescriptorProtoSchema) });
const messages = [create(FileDescriptorProtoSchema, { name: 'example.proto' })];

const result = plan(env, parse('repositories.map(x, x)'))({ repositories: messages });
if (isCelError(result)) throw result;

const values = [...result];
console.log(values.length);

Output:

1

An identity map(x, x) should not be necessary to make the evaluation result iterable.

Environment

  • @bufbuild/cel version: 0.6.2
  • @bufbuild/protobuf version: 2.15.0
  • @bufbuild/cel-spec version: not used
  • Node.js version: v26.13.1

Possible Solution

From inspecting the implementation, the failure appears to involve deferred conversion and the evaluation context lifetime:

  1. ArrayList.values() calls toCel(element) when an element is read.
  2. For a Protobuf message, toCel() calls reflect(getMsgDesc(v.$typeName), v).
  3. getMsgDesc() obtains the registry through getEvalContext().
  4. By the time the caller reads the returned list, withEvalContext() has already removed the context in its finally block.

It may be necessary to preserve the information needed for deferred conversion in returned values, or complete that conversion before leaving the evaluation context. I have not implemented or validated a library-level fix.

Workarounds

Both approaches below have been verified with the versions listed in Environment. They are caller-side workarounds, not a library-level fix.

1. Force conversion during evaluation with an identity map

The passing example above uses repositories.map(x, x). Reading each element during evaluation lets the library resolve its message descriptor while the evaluation context is active. This requires changing the CEL expression. For large lists, the separate performance issue in #343 is a concern.

2. Supply ReflectMessage values using the public reflect() API

Keep the original CEL expression and wrap each input message with its schema before evaluating. Save this independent example as reflect-workaround.mjs and run node reflect-workaround.mjs:

import { celEnv, plan, parse, isCelError } from '@bufbuild/cel';
import { create, createRegistry } from '@bufbuild/protobuf';
import { reflect } from '@bufbuild/protobuf/reflect';
import { FileDescriptorProtoSchema } from '@bufbuild/protobuf/wkt';
const env = celEnv({ registry: createRegistry(FileDescriptorProtoSchema) });
const message = create(FileDescriptorProtoSchema, { name: 'example.proto' });
const result = plan(env, parse('repositories'))({
  repositories: [reflect(FileDescriptorProtoSchema, message)],
});
if (isCelError(result)) throw result;
const values = [...result];
console.log(JSON.stringify({ count: values.length, sameMessage: values[0].message === message }));

Output:

{"count":1,"sameMessage":true}

The values already carry their message descriptors, so reading this result after evaluation does not need to look them up through the evaluation context. The original Message is preserved, as the identity check demonstrates.

This avoids the added CEL identity map, but callers must have the appropriate schemas and prepare all relevant input messages, including those in nested input containers or intermediate bindings. Preparing those inputs also has a traversal/allocation cost. This example does not establish that every CEL input shape is covered, and I have not benchmarked the two approaches end to end.

主要言語
TypeScript
スター
43
フォーク
9
平均マージ
1日 2時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bufbuild/cel-es のほかの issue

bufbuild/cel-es の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。