Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp436743pxa; Fri, 21 Aug 2020 11:03:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtODefbZrU/KNftfsX0i/JAZv98b2ux2UxddB+zRdD4pelZWFR2tp8LdpcCwMYI5I9daeT X-Received: by 2002:a17:906:7856:: with SMTP id p22mr3951822ejm.262.1598032990907; Fri, 21 Aug 2020 11:03:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598032990; cv=none; d=google.com; s=arc-20160816; b=Lb1/7d6JIS90p1qEUWE1FssP3Qx8O9qzOK8FAWdSyXZPPvsYet6liTvXIrEJmaVNt6 JyFLiUaW9NGyIz6RnroujoxVIlvkWDAGKqLG5b2KQi/iKiZocRPEcdXoxQT1FXll69hA 10CVYZo1ze3kEuubPmKx84FwQcq/IyNHp1Wn+xqpWQ+fmXu3VGq3hnsxOkN8UJionjAe dm/JUUaplr4Q1npDMRd1M3e23vFMBDwjtiXUPtTYqwKwIFFbh07og+evHOTYT3osR5KX nsMMRheKpUhnCGXA/xsSKSbAoACkbW3oiyw4x38+37FDLrzKMWyCZwvAzhqOiIFn7geh kWDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8Khob2YLsczWPVhqP71ZFU0FcOyus14dQulSnyUMpZU=; b=sagrdXYJXhcWsxlVK2GRUCToAnl6G/cACozJVtKXplZf29g/z5xo4Gt409E6Adne60 EWACiAnCYxGj54D5Aaac1xKC/mIxgWaqcn32qK5xLHkF2y8n5JIdI03FrxAjbJuPbCZM 3ldqfx+cmcQjrylcwIXBfM8XdqN/O5Cnd6XDncg5s2+5R93Me/QRlXu3f5AgGIdao22T AaxWgmllogFv4ISWh8YvaO6xObU4iixsoT8ypowpjNNtYF7eKQHF3aGhV7fH+Dokj0gk 0Las3FJe8f0ZCVW98oYb5DKMCpeVdvGdX6wPVHX4jKhYhzAu5mRALsUCA2EQUDGM7LF5 703Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VTfsXh9H; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k21si1569713eje.480.2020.08.21.11.02.44; Fri, 21 Aug 2020 11:03:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VTfsXh9H; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726002AbgHUSBQ (ORCPT + 99 others); Fri, 21 Aug 2020 14:01:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725976AbgHUSBO (ORCPT ); Fri, 21 Aug 2020 14:01:14 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05501C061573 for ; Fri, 21 Aug 2020 11:01:10 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id a79so1440141pfa.8 for ; Fri, 21 Aug 2020 11:01:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8Khob2YLsczWPVhqP71ZFU0FcOyus14dQulSnyUMpZU=; b=VTfsXh9HapOpKLuD0Fxxt0Smw0lYAGLy/xMmbg8TfhtCJzz8z88UpIe4vH5cW2+vQD pGwsKqx524N13PhnmOcsWm0lc45E5EuLfk5QfL5uiBB3MltjB2rmf2j8Um+6IIyIESDg ZD9y4ed7YytQUDap+nyHGPTCdQOS/mY2EJWvg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8Khob2YLsczWPVhqP71ZFU0FcOyus14dQulSnyUMpZU=; b=UdSixIk9EwZZGakvdqZLRy1kTTQrEGTNodUjVSVvvgmscYC/o/tHvw3Suy/sXHKFDV +SmksC87U+XlqpB/Zg+tOq9wzo5Ui2tdCFTPLQ2cPR2z85J2+cf4469t0RWyWsaWMZPQ VG2Ys+Xv1q1zcPuLYxLitRTB8nWMzhiZzeQYVU3zLY0FfyizTVHf69H/pstQVPbnvlHQ 5MfhAweoighPWkv3hRp4SKdTIt7gIpxdqgL12V9EpnoammKqjONlvBP70VbZUNafe2pv NiaZKDYZMuxTKCDPA4ld5zqy1PGWQSkQcU2IqEtn/5uUPLeDcbhnKHriGnOJ+YwqZtib RVIw== X-Gm-Message-State: AOAM5322/6deaJfTr7IPKk2jOjvABuMqfm5lNf7pcISAXASxjGtKd54m fEn/BuxaA+ntoyQwxPgnPGrvPFyD7K8meg== X-Received: by 2002:aa7:9467:: with SMTP id t7mr3322402pfq.64.1598032869268; Fri, 21 Aug 2020 11:01:09 -0700 (PDT) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id s22sm3075558pfh.16.2020.08.21.11.01.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Aug 2020 11:01:08 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v2] device: Fix race condition between device connection and disconnection Date: Fri, 21 Aug 2020 11:01:06 -0700 Message-Id: <20200821180106.21048-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org When Connect() is called and waiting for return, dev_disconnected may be called due to MGMT_EV_DEVICE_DISCONNECTED event from kernel. In that case reply to client that the connection failed otherwise the dbus method will timeout because bluetoothd never replies. Tested with simulation of a lot of Connect() to bluetooth devices and check that error is returned from bluetoothd rather than dbus timeout when this race condition happens. Reviewed-by: Miao-chen Chou --- src/device.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/device.c b/src/device.c index 7b7808405..f59b6c1d0 100644 --- a/src/device.c +++ b/src/device.c @@ -3006,6 +3006,7 @@ void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type) void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type) { struct bearer_state *state = get_state(device, bdaddr_type); + DBusMessage *reply; if (!state->connected) return; @@ -3020,6 +3021,18 @@ void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type) device->disconn_timer = 0; } + /* This could be executed while the client is waiting for Connect() but + * att_connect_cb has not been invoked. + * In that case reply the client that the connection failed. + */ + if (device->connect) { + DBG("connection removed while Connect() is waiting reply"); + reply = btd_error_failed(device->connect, "Disconnected early"); + g_dbus_send_message(dbus_conn, reply); + dbus_message_unref(device->connect); + device->connect = NULL; + } + while (device->disconnects) { DBusMessage *msg = device->disconnects->data; -- 2.26.2