diff options
author | Juha Vuolle <juha.vuolle@insta.fi> | 2022-05-13 07:48:40 +0300 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@insta.fi> | 2022-05-30 13:27:25 +0300 |
commit | 336bd4ff9edfdb3e5607a6e74af663b409e0c852 (patch) | |
tree | e86e5269e994a5e4ba6a02ff9975cfa2690db0cf /src | |
parent | ca96a42035255b4355e4d8ddba7f5da7b9006577 (diff) |
Close socket descriptor when QBluetoothSocketBluez is destroyed
There are two private QBluetoothSocket backends on Linux:
- QBluetoothSocketBluez is native linux socket implementation
It is always used by the linux QBluetoothServer, and by
QBluetoothSocket if Bluez version is < 5.46
- QBluetoothSocketBluezDbus used by QBluetoothSocket when
Bluez >= 5.46
Leaving the native socket unclosed leaks the resource and eventually
we may run out of descriptors. This is reproducible by creating and
destroying QBluetoothServer instances in a loop.
As a related drive-by:
- Fix bluetooth socket autotest version check. DBus socket is used
with bluez 5.46+ (for clarity: DBus lowenergycontroller
is used with bluez 5.42+). This is needed for the test to pass
with Bluez < 5.46
- Add a clarifying comment on socket close()
Fixes: QTBUG-103067
Change-Id: Idc38c743be09e559ea82bf09c2f9e44e4b80d666
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
(cherry picked from commit 3aafe9d5ce117858143dbb527f742cf875aa83e8)
Diffstat (limited to 'src')
-rw-r--r-- | src/bluetooth/qbluetoothsocket.cpp | 3 | ||||
-rw-r--r-- | src/bluetooth/qbluetoothsocket_bluez.cpp | 6 |
2 files changed, 9 insertions, 0 deletions
diff --git a/src/bluetooth/qbluetoothsocket.cpp b/src/bluetooth/qbluetoothsocket.cpp index 864b9cab..5499c7d7 100644 --- a/src/bluetooth/qbluetoothsocket.cpp +++ b/src/bluetooth/qbluetoothsocket.cpp @@ -791,6 +791,9 @@ void QBluetoothSocket::close() Set the socket to use \a socketDescriptor with a type of \a socketType, which is in state, \a socketState, and mode, \a openMode. + The set socket descriptor is considered owned by the QBluetoothSocket + and may be e.g. closed once finished. + Returns true on success */ diff --git a/src/bluetooth/qbluetoothsocket_bluez.cpp b/src/bluetooth/qbluetoothsocket_bluez.cpp index f516629e..0240edbf 100644 --- a/src/bluetooth/qbluetoothsocket_bluez.cpp +++ b/src/bluetooth/qbluetoothsocket_bluez.cpp @@ -75,6 +75,10 @@ QBluetoothSocketPrivateBluez::~QBluetoothSocketPrivateBluez() readNotifier = nullptr; delete connectWriteNotifier; connectWriteNotifier = nullptr; + + // If the socket wasn't closed/aborted make sure we free the socket file descriptor + if (socket != -1) + QT_CLOSE(socket); } bool QBluetoothSocketPrivateBluez::ensureNativeSocket(QBluetoothServiceInfo::Protocol type) @@ -658,6 +662,8 @@ qint64 QBluetoothSocketPrivateBluez::readData(char *data, qint64 maxSize) void QBluetoothSocketPrivateBluez::close() { + // If we have pending data on the write buffer, wait until it has been written, + // after which this close() will be called again if (txBuffer.size() > 0) connectWriteNotifier->setEnabled(true); else |