From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from DU2PR03CU002.outbound.protection.outlook.com (mail-northeuropeazon11021119.outbound.protection.outlook.com [52.101.65.119]) by lore.virtuozzo.com (Postfix) with ESMTPS id 8BC8D803BB for ; Mon, 16 Nov 2026 13:35:24 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=twwTaar6tg2kQi49Hc/eMHWC5qquXLeU5ddMnBCtah3icGaDL/hoFEsD438E/ZbljNYvbcHCrwwPJe+SmB4ObIiBtU2N15ZMh4k1FC+IWrPS3e0IeN5fkQZxVq8a8XA+mG3wi2cjbmiIv8RflRfrMO53E54pExVNGtpPuIOLbqilLj+/N9UBczSjw6exQqvVmDxVeHydF6KVCtbjMQOeOhRvspXgIGUiF9z9Xbp0OX0i938bra6ZHKP8oY3kAN6RDPFHPcJF7xAY48YDwnF4moLqnnk2kT9BVjM0awza/MJjDMLJFoh2veUanBv2NwkNearrseawrLsHw9JLx4dWug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=no4CzDX/3bRdADE2D+tH1/oaMoTfX0iExmYJLzr8a2Y=; b=YaEEMh0azP9Bv5ijJxdQXFmyLlMH4gAOC8YFyBhMPQT4pOe/Q+moGwJWNggiWxT8rhqF9Vm1yRYi1TCEHNf448+Tn7KQx649Bq54G2bJei39xFKf4LbXol474MZUnZrW6OpG2APxlRbPq/RHviEEFKAjfh6kCWCpAdot4xcllROfdDCCtXCFoMcT7QVhGGah0HQ0BLbBfchm2imLmUVK7XxOx+IDlzzvcLaN8+UgwwIM4OiEFjTByLq5YZMIpkYTKXYd4o5TpOTblRdc4HuF3mX2dQdc5yAuRV3Xiiywk7m1CWgxLxAwB+pj5ev+ivV9WrZxIEo2i+qlMkon1rr05w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 130.117.225.111) smtp.rcpttodomain=virtuozzo.com smtp.mailfrom=virtuozzo.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com; arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=no4CzDX/3bRdADE2D+tH1/oaMoTfX0iExmYJLzr8a2Y=; b=Uj4486BnSwzZqlsWBraMIXwlVsS3lV5DcrJF9SXqYrZXLIJkNtxAAfYfjSjalEbbWqon8p5rz/d1iNIHcYnxiQBzyzrxX0Az3aQnZwjxT3MpJbuK3eAZ5F94VHJWJ+yUTYMHoIVpd6ko0TlDvGm59yDCultHrrReO9lKFkWRImdqNd2sOfxwbs4XuBbyzNblIZG80oAkt9PESSgkmoY9bkSpBgTqRRsp9zGOIJ11YhXpJh8+6H+xHNgsmLDG9AaahWT2UGAIchJmgTy6YCEq7ceaPexyGxWgCgLMMUvsMvLQE6XcGUxy5c35JG5Zk3U35LUVkixDIlPxiYLxg/NStQ== Received: from OS6P279CA0051.NORP279.PROD.OUTLOOK.COM (2603:10a6:e10:31::9) by AS2PR08MB8478.eurprd08.prod.outlook.com (2603:10a6:20b:55c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.615.14; Mon, 16 Nov 2026 13:35:20 +0000 Received: from CPH1EPF0000051E.eurprd04.prod.outlook.com (2603:10a6:e10:31:cafe::45) by OS6P279CA0051.outlook.office365.com (2603:10a6:e10:31::9) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.615.14 via Frontend Transport; Mon, 16 Nov 2026 13:35:19 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 130.117.225.111) smtp.mailfrom=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com;dmarc=pass action=none header.from=virtuozzo.com; Received-SPF: Pass (protection.outlook.com: domain of virtuozzo.com designates 130.117.225.111 as permitted sender) receiver=protection.outlook.com; client-ip=130.117.225.111; helo=relay.virtuozzo.com; pr=C Received: from relay.virtuozzo.com (130.117.225.111) by CPH1EPF0000051E.mail.protection.outlook.com (10.167.241.233) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.615.4 via Frontend Transport; Mon, 16 Nov 2026 13:35:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Content-Type:MIME-Version:Message-ID:Date:Subject :From; bh=no4CzDX/3bRdADE2D+tH1/oaMoTfX0iExmYJLzr8a2Y=; b=jmWFGCCkmeQO8clYTBo UAfsnL0Zj59GmooCmHGAfuImM1XJXP4WFLoVoActkFn7PuAkgqKvwjzluJwjho6Dm8ERaZwdeSmSY yStkylD0QaarG5Jf+V96CKD3qZjxAxy5FOAuJP4GqjhFjciSmRG0lFZi2PWhR8YnlAIj9zub+Fm0K u3sQLmnWXHpmL785mTXpuInSQKrH2j6XvSNSGlho8F4BVHJfKrctzkUpfMvJGzgjUxljtz/T2QgGT W9t5cti+cW4Bvt0prlH6m2AYavKsO4nCUF9U50x9l8iBxyrbaQ7ROrJbG6/WiJEogCsSH9rgnxmq2 hbBVAkTB6niAiNw==; Received: from [130.117.225.5] (helo=vz9-demens-1.aci.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xSwnQ-003VJw-0N; Mon, 16 Nov 2026 14:35:10 +0100 From: Andrey Zhadchenko To: andrey.drobyshev@virtuozzo.com Cc: svt-core@virtuozzo.com, den@openvz.org Subject: [PATCH hci-dev] monitor/qmp: cleanup SocketChardev listener sources early to avoid fd handling race #VSTOR-155186 Date: Mon, 16 Nov 2026 16:30:56 +0300 Message-ID: <20261116133056.1046875-1-andrey.zhadchenko@virtuozzo.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CPH1EPF0000051E:EE_|AS2PR08MB8478:EE_ X-MS-Office365-Filtering-Correlation-Id: a5e7bc28-68f5-46f3-b34c-08df43e67518 X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr List-Id: svt-core@virtuozzo.com X-MS-Exchange-Transport-Rules-Loop: 1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|23010399003|82310400026|36860700016|10067099003|261026121527099003|261026140227099003|261028144727099003|261021123027099003|261021121427099003|261022075727099003|261022105027099003|261102123227099003|261009223027099003|57112099003|18002099003|5113699003|6133799003; X-Microsoft-Antispam-Message-Info: rSq6m12wRk/LzutFdgO3Tur3tKIochuBaFXL7ClZRsASFsDlRaBTw2tC3Wswj6PIkpiYsnTvr5bV7TEIAKYVJ5i2hXl+bleFoW83BsRrVt/ffoW2XUO1fp77FgPUrPjYS6ZFD/XsfF9+rLEM9wiH7/QmolCAljSh3hj7E+H2trkuuRRJ63Hu13BbTvu1l5g4DigfEUGPIKh1F7NfH9/LCAwd4fmuMqamhIIhGFY76/OoYd3h/dHknZ4tEkFQUD7SOKjI+qyog819pmH02ieCSlBzXPRX5oX4FgaKD0Gqr6Nr6PYrORL0xz5zJvETD77mLWKlg1f4SQoJGbOep09P7d9gXBhMcHzzZf6GSeuj3ohknsnhHE2FdLmd4b0tVQc17C5IjSKHd6aehqju8YBJ8xtWeGeJTvrIYPrrvqQkrVFHFqbo8iAHHy8me8blZP/1cvX7cxiK+Z6UmGL4xzMsaVRJmOlnaAZXRg1mput9vobX+2nS58JEUTTfIhz0HcEfhce+Q5VChaEBV550HfFuwLSpvPjThaVJGDUrnmEGzCv70/Xzsd55GzU2nPFVY09zx/xlxf7qJeQEObXlBQevAHlv7LqFAC5tEqnuxc/XDqS8w2JVWPXpV/x03RcBo4g646qe7V4EplCBRnB2xEOOAfPJ4yea2+2Xwbyr2/qfNvCg2Nn1UplAGG3ETXrSd4f7cHjCQExMhOgaEskUHebLTsZhBdzp8lXDSoYyGfbNYwftI3GSd0GnciGZ6419N8g9y9Ta7R5hIap0L9chZX7JvncmIwC0wNq4YUPOZ5qu1/E/dFJ8RzqIqd4IBcTMBHz6DhvS6zrUIXaiSo8ao15f4RCUG9fW6BrbioSO6bd5Ngl+DSge/2rWybKmWFxbXazPS+YpEGKC964OyKqDD8azs83uNMaaRCOp2RfUpHR0V0o= X-Forefront-Antispam-Report: CIP:130.117.225.111;CTRY:CH;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:relay.virtuozzo.com;PTR:relay.virtuozzo.com;CAT:NONE;SFS:(13230040)(1800799024)(376014)(23010399003)(82310400026)(36860700016)(10067099003)(261026121527099003)(261026140227099003)(261028144727099003)(261021123027099003)(261021121427099003)(261022075727099003)(261022105027099003)(261102123227099003)(261009223027099003)(57112099003)(18002099003)(5113699003)(6133799003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: swmqw9yC5lxqsNg4XV+9mlaDDtIejsUYqPhOaz0SDe+c2G993BvmalpvfBjQ941DYiBySc5JbC7XiKrVF5xRJUrgur5Cndo3cPTV5jrOxh2Vy/e8wUNW6y3Zw/ANNxeXjVhIHdrRK4KgI/1Xq+KMUST287C7cHp2IgZkJS+rKgKLvexHsQIpei/1GRm6SHc5E6Twusk36/pXvyh0iLzZuN7q4wSyQ156iKuF3pNvwlgc1WkNRZMOlUHKEwjKBRxFtv3qM2OdlXPBYwzqzQ0jv5TkQBoFDjzbjLxF5sWUkv418i9kxKZuFXeBTlW+BjTTq0Yphcqw3NFjC+gHxojEI15xR63HKJP3pLndO+jWASWScFhSvwhNs7sP3bcGb43Wbpl6fEwIOaHyCXS1m8araXfhKruwADeasS1wy4Oq0HuUyts6wqW5aUYBmvJtR+RQ X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Nov 2026 13:35:19.5412 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a5e7bc28-68f5-46f3-b34c-08df43e67518 X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=0bc7f26d-0264-416e-a6fc-8352af79c58f;Ip=[130.117.225.111];Helo=[relay.virtuozzo.com] X-MS-Exchange-CrossTenant-AuthSource: CPH1EPF0000051E.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB8478 From: Jie Song When starting a dummy QEMU process with virsh version, monitor_init_qmp() enables IOThread monitoring of the QMP fd by default. However, a race condition exists during the initialization phase: the IOThread only removes the main thread's fd watch when it reaches qio_net_listener_set_client_func_full(), which may be delayed under high system load. This creates a window between monitor_qmp_setup_handlers_bh() and qio_net_listener_set_client_func_full() where both the main thread and IOThread are simultaneously monitoring the same fd and processing events. This race can cause either the main thread or the IOThread to hang and become unresponsive. Fix this by proactively cleaning up the listener's IO sources in monitor_init_qmp() before the IOThread initializes QMP monitoring, ensuring exclusive fd ownership and eliminating the race condition. Signed-off-by: Jie Song Reviewed-by: Marc-André Lureau Message-ID: <20251125140706.114197-1-mail@jiesong.me> (cherry picked from commit e714f1a3d4d1e66b9a3ff4be1ff999c32bbef29e) --- chardev/char-io.c | 8 ++++++++ chardev/char-socket.c | 10 ++++++++++ include/chardev/char-io.h | 2 ++ include/chardev/char.h | 2 ++ monitor/qmp.c | 5 +++++ 5 files changed, 27 insertions(+) diff --git a/chardev/char-io.c b/chardev/char-io.c index d9b11f335ff..0e5ba316ed9 100644 --- a/chardev/char-io.c +++ b/chardev/char-io.c @@ -187,3 +187,11 @@ int io_channel_send(QIOChannel *ioc, const void *buf, size_t len) { return io_channel_send_full(ioc, buf, len, NULL, 0); } + +void remove_listener_fd_in_watch(Chardev *chr) +{ + ChardevClass *cc = CHARDEV_GET_CLASS(chr); + if (cc->chr_listener_cleanup) { + cc->chr_listener_cleanup(chr); + } +} diff --git a/chardev/char-socket.c b/chardev/char-socket.c index c73a3dd0f7a..7eaa4f9301c 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1695,6 +1695,15 @@ static void char_socket_cpr_claim_fds(Chardev *chr) } } +static void tcp_chr_listener_cleanup(Chardev *chr) +{ + SocketChardev *s = SOCKET_CHARDEV(chr); + if (s->listener) { + qio_net_listener_set_client_func_full(s->listener, NULL, NULL, + NULL, chr->gcontext); + } +} + static void char_socket_class_init(ObjectClass *oc, const void *data) { ChardevClass *cc = CHARDEV_CLASS(oc); @@ -1713,6 +1722,7 @@ static void char_socket_class_init(ObjectClass *oc, const void *data) cc->chr_add_watch = tcp_chr_add_watch; cc->chr_update_read_handler = tcp_chr_update_read_handler; cc->cpr_claim_fds = char_socket_cpr_claim_fds; + cc->chr_listener_cleanup = tcp_chr_listener_cleanup; object_class_property_add(oc, "addr", "SocketAddress", char_socket_get_addr, NULL, diff --git a/include/chardev/char-io.h b/include/chardev/char-io.h index ac379ea70e3..540131346d1 100644 --- a/include/chardev/char-io.h +++ b/include/chardev/char-io.h @@ -43,4 +43,6 @@ int io_channel_send(QIOChannel *ioc, const void *buf, size_t len); int io_channel_send_full(QIOChannel *ioc, const void *buf, size_t len, int *fds, size_t nfds); +void remove_listener_fd_in_watch(Chardev *chr); + #endif /* CHAR_IO_H */ diff --git a/include/chardev/char.h b/include/chardev/char.h index 968766fd5d5..62cfdc2d3d7 100644 --- a/include/chardev/char.h +++ b/include/chardev/char.h @@ -313,6 +313,8 @@ struct ChardevClass { /* handle various events */ void (*chr_be_event)(Chardev *s, QEMUChrEvent event); + + void (*chr_listener_cleanup)(Chardev *chr); }; Chardev *qemu_chardev_new(const char *id, const char *typename, diff --git a/monitor/qmp.c b/monitor/qmp.c index 22e253bfcd4..bb89141baca 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -549,6 +549,11 @@ void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) * e.g. the chardev is in client mode, with wait=on. */ remove_fd_in_watch(chr); + /* + * Clean up listener IO sources early to prevent racy fd + * handling between the main thread and the I/O thread. + */ + remove_listener_fd_in_watch(chr); /* * We can't call qemu_chr_fe_set_handlers() directly here * since chardev might be running in the monitor I/O -- 2.43.5