Bug #121801 Server crash after stored function cursor over UNION view is reprepared following ALTER TABLE

Submitted: 17 Nov 6:09
Reporter: Wenbin Wu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Stored Routines Severity:S2 (Serious)
Version:8.0.32,8.0.45 OS:Linux (Official mysql:8.0.45 Docker image)
Assigned to: CPU Architecture:ARM (ARM64 host/container)
Tags: alter-table, crash, cursor, reprepare, stored-routine, UNION

[17 Nov 6:09] Wenbin Wu
Description:
MySQL Community Server 8.0.45 terminates when a cached stored function is reprepared after metadata invalidation.

The stored function declares a cursor whose SELECT reads from a UNION view joined with another table and compares columns with stored-function parameters. The first invocation succeeds. After ALTER TABLE changes the joined table metadata, the next invocation terminates the server connection and the official Docker container exits.

Expected result:
The second invocation completes normally and returns "high", just like the first invocation.

Actual result:
The client reports ERROR 2013 (HY000): Lost connection to MySQL server during query. The mysql:8.0.45 container terminates and docker exec exits with status 137.

Verified environment:
- MySQL Community Server 8.0.45, official Docker image mysql:8.0.45
- Linux container on ARM64
- mysql command-line client 8.0.45
- No explicit container memory limit

The same scenario was originally observed on 8.0.44. I independently reproduced it on 8.0.45. A control case using a UNION derived table with constant predicates instead of stored-function local parameters remains healthy before and after ALTER TABLE.

How to repeat:
Run the following statements in one mysql client session. The final SELECT causes the server/container to terminate.

DROP FUNCTION IF EXISTS getFormEnumName;
DROP VIEW IF EXISTS v_all_code_table;
DROP TABLE IF EXISTS code_t1, code_t2, fm_formfield;

CREATE TABLE code_t1 (
    id INT,
    companyId INT,
    tableId INT,
    name VARCHAR(64)
);
INSERT INTO code_t1 VALUES
    (2, 1, 101, 'high'),
    (9, 2, 109, 'other');

CREATE TABLE code_t2 (
    id INT,
    companyId INT,
    tableId INT,
    name VARCHAR(64)
);
INSERT INTO code_t2 VALUES (9, 2, 109, 'other');

CREATE VIEW v_all_code_table AS
SELECT id, companyId, tableId, name FROM code_t1
UNION
SELECT id, companyId, tableId, name FROM code_t2;

CREATE TABLE fm_formfield (
    referenceid INT,
    storefield VARCHAR(200),
    companyid INT,
    fieldid INT
);
INSERT INTO fm_formfield VALUES (1, 'f01', 1, 101);

DELIMITER |
CREATE FUNCTION getFormEnumName(
    iCompanyID INT,
    iFormSchema INT,
    iStoreField VARCHAR(200),
    iFormValue INT
)
RETURNS VARCHAR(4000) CHARSET utf8mb4
READS SQL DATA
BEGIN
    DECLARE m_name VARCHAR(500);
    DECLARE returnValue VARCHAR(4000);
    DECLARE done INT DEFAULT FALSE;

    DECLARE cur CURSOR FOR
        SELECT v_all_code_table.name
        FROM v_all_code_table, fm_formfield
        WHERE fm_formfield.referenceid = iFormSchema
          AND fm_formfield.storefield = iStoreField
          AND v_all_code_table.companyId = fm_formfield.companyid
          AND v_all_code_table.tableId = fm_formfield.fieldid
          AND v_all_code_table.id = iFormValue
          AND v_all_code_table.companyId = iCompanyID;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;
    label: LOOP
        FETCH cur INTO m_name;
        IF done THEN
            LEAVE label;
        END IF;
        IF returnValue != '' THEN
            SET returnValue = CONCAT(returnValue, ',', m_name);
        ELSE
            SET returnValue = m_name;
        END IF;
    END LOOP label;
    CLOSE cur;
    RETURN returnValue;
END|
DELIMITER ;

-- Succeeds and returns high.
SELECT getFormEnumName(1, 1, 'f01', 2) FROM fm_formfield;

-- Invalidates table metadata / triggers reprepare on next invocation.
ALTER TABLE fm_formfield ADD COLUMN extra INT;

-- Reproduces: ERROR 2013 and server/container termination.
SELECT getFormEnumName(1, 1, 'f01', 2) FROM fm_formfield;

Suggested fix:
Please ensure that reprepare of a stored-program cursor fully rebuilds the UNION query expression and all Item_splocal/local-parameter references after metadata invalidation. Please add an MTR regression test that invokes the function, alters a referenced table, invokes it again, verifies the returned row, and verifies that the server remains alive.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.