Skip to content

[Bug]: concurrent NEXTVAL returns context canceled instead of unique values #28246

Closed

Description

@Ariznawlll

问题描述

两个独立连接同时对同一个新 sequence 执行一次 NEXTVAL 时,MatrixOne 不会让两个调用分别取得唯一值,而是一个调用成功、另一个调用直接返回通用的 context canceled。在 10 个独立 sequence 上重复该最小场景,10/10 次均出现相同行为。

扩大到 4 个连接、每连接连续调用 20 次时,只有一个连接取得 1..20,另外三个连接的 sequence 调用均返回 context canceled。没有观察到重复值,但 sequence 在并发取号时不可用。

环境

  • MatrixOne latest main
  • commit: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • 单机本地部署,4 CN 扩展或大数据量不是触发条件

最小复现方式

create database seq_concurrent;
use seq_concurrent;
create sequence s increment by 1 start with 1 no cycle;

随后在两个新连接中以 barrier 同时执行:

select nextval('s');

当前行为

每轮结果均为:

connection A: 1
connection B: ERROR 1105: context canceled

成功连接并不固定。失败连接紧接着执行 SELECT 1 可以成功,说明连接仍有效;错误仅由并发 NEXTVAL 路径暴露。sequence 状态为 last_seq_num=1, is_called=true。

测试为每轮创建独立 sequence、两个新连接、每连接只调用一次,共重复 10 轮,全部复现。

期望行为

并发 NEXTVAL 应原子地分配不同的 sequence 值,例如一个连接得到 1、另一个得到 2。底层写冲突如果需要重试,应在 sequence 实现内部完成,不应向用户返回无上下文的 context canceled。

代码线索

pkg/sql/plan/function/seqfunc.go 当前先通过内部 SQL 读取 sequence 行,再通过另一条内部 SQL 更新 last_seq_num/is_called。并发调用会竞争同一行,当前路径没有把冲突处理为安全重试或原子取号。

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. Ariznawlll commented on Sep 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试环境:MatrixOne 最新 main f72ca9efbeb3a7c673701e4a135cc6670c33cfde,本地单机服务(MySQL 协议 6001)。

    测试步骤:

    1. 每轮创建独立 sequence,并建立两个独立客户端连接。
    2. 使用同步屏障同时发起两次 NEXTVAL。
    3. 连续执行 10 轮,检查返回值唯一性、是否出现 context canceled,并清理测试对象。

    测试结果:10/10 轮均返回唯一值 1、2,未出现 context canceled,清理成功。

    结论:问题已修复。修复提交 #28345(074bc173a5b4)已包含在当前 main;对应 MOTR Python 场景已加入 PR #147(https://github.com/matrixorigin/motr/pull/147),定向运行通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions