Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp59273pxa; Fri, 21 Aug 2020 00:45:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwt3Y4LGe/23eh5kmeet9rb546M//5dD9gr2T1ULklUfAzDRDeiin737kGxyg5s1KewJcTF X-Received: by 2002:a17:906:abc6:: with SMTP id kq6mr1683594ejb.216.1597995947594; Fri, 21 Aug 2020 00:45:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597995947; cv=none; d=google.com; s=arc-20160816; b=ddlAJ6rW3iUbzt3LPPeJ82q8NMZ7Lv9SvgvtDM2JzikN4ncoQdo1e2VWgat1l0U0D4 cHwZm8uwvDu3I4GNXy8iERYTk59z73espQNVAEg7GHqic5aWwdqKnf6gUenD8VVsm4Dv VjUcCCCXDwJ5aWJunW4rjQjLIC/nBsuIyfgbC83aRP1dwEhYqh6bWVsQ0FBjXJLeqWk2 ahlbhcORWyUypxU8LftIX7eiJUqYL3tlTzAY+XQRDhTTrrCG6tZtW4EVPRZ9Npd5yVoq V+SFivUJc4ttqclfSd9uENz4Q7qCwxo+nQ6+MJ73Rxij3C9DNTlXHn073B48LV/9Ojqo foFA== 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=ahNzFwqsXNzsJ6fzog/DY5DWWrZLWO8A13L3oZbfyg8=; b=cTzavOhs8dyogZlJOZN+tyG84f/6Xr4BbMiQivIeMT3YVaj9cRaoAe5jLBdsbuU8Qd P7V73Fr/wcsa3pwzpzwrSPgZJx7Il59XMtl12MH8CFYGkY8//64WbxtM519BqEtuosYl vYn43xVbzQk0U0OpCu08pIuitVYUuJSLqsUs6P7OXfkISbGYFOjOGhXwNYhCYx3Jovpk JdRIByiys2r3d3ojmzBBDayGpvrRON6/5kt1z7RT9NITKzWB3egjPDaxxXKmUGsPUi2d VodrGip6+q8ZJ4vZMBpj97JKStuSYWH6vbM/bX0u02vHsO228crpGcWw2xYIKfj9JoiP S64g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="X7y9Y/AN"; 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 lo6si625883ejb.584.2020.08.21.00.45.21; Fri, 21 Aug 2020 00:45:47 -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="X7y9Y/AN"; 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 S1726332AbgHUHpR (ORCPT + 99 others); Fri, 21 Aug 2020 03:45:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725908AbgHUHpQ (ORCPT ); Fri, 21 Aug 2020 03:45:16 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2837C061385 for ; Fri, 21 Aug 2020 00:45:15 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id h12so617725pgm.7 for ; Fri, 21 Aug 2020 00:45:15 -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=ahNzFwqsXNzsJ6fzog/DY5DWWrZLWO8A13L3oZbfyg8=; b=X7y9Y/AN/biKpTBgZSJBaxsFJAYgnfRDFH6DvAqxyp/vs9MS8VM0tATkIeoz0ntKYY k5JI4rp5GaOWQ2Xg38/I5nYfCNCrUV05+xV8pRPDAH3Nj2MMG4JgSRnXMi1KpFXsS0lm 95EYdzLICPpRv5dYmJWT0+2MzythZHCw35KfE= 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=ahNzFwqsXNzsJ6fzog/DY5DWWrZLWO8A13L3oZbfyg8=; b=ENI4Y4f2ygUpQWftCSO0+R8naX4zwz7APPc/TZ8eFFDZlXR9qu9VA6WyJM8J3LeuDv p2JIakOtmxbxZHmLaKkY9kMM/W1YkzBaeChY6k9b+S1RyA81VdXDCUXhQrT7IdAan4sb G8b7B4zZCc0g/vZAC03JGdGh5A7RmwYfjX4VXqgLpN4iMOVqRSqh9+E0mvFAsL8ZUU+x 9WXIMi6g8wUOZcub7shWzs6Dfs5tx+8J/HIiQGe5kKQuK/kGxjO3QHNKnQ9dZXP5M8IU H8xmDJ4/qewAEDmNt0IYq6j8Pw7Ta/T39cjQcgU9ds4YIDv2/1oMYn7tFs8ASTk4Opo4 K5Uw== X-Gm-Message-State: AOAM531j7LuZ56lSIy2zjF1+7gMnHyIHcx11m5Hc9BAyZjD9ATTqxsXR qPbMBLe40xZEQRhGUGOCgfjruZ7FiAL03A== X-Received: by 2002:a63:3710:: with SMTP id e16mr1454661pga.140.1597995915145; Fri, 21 Aug 2020 00:45:15 -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 w14sm1628078pfj.220.2020.08.21.00.45.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Aug 2020 00:45:14 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ] device: Fix race condition between device connection and disconnection Date: Fri, 21 Aug 2020 00:45:12 -0700 Message-Id: <20200821074512.19985-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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/device.c b/src/device.c index 7b7808405..55e7b4042 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,17 @@ 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