Skip to content

Commit c4d2f51

Browse files
committed
PluggableDiscovery: correct synchronization on 'portList' access
1 parent 1b2bca0 commit c4d2f51

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

arduino-core/src/cc/arduino/packages/discoverers/PluggableDiscovery.java

+20-22
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ private void processJsonNode(ObjectMapper mapper, JsonNode node) {
139139
return;
140140
}
141141

142-
portList.clear();
142+
synchronized (portList) {
143+
portList.clear();
144+
}
143145
portsNode.forEach(portNode -> {
144146
BoardPort port = mapJsonNodeToBoardPort(mapper, node);
145147
if (port != null) {
@@ -246,45 +248,41 @@ private void write(String command) {
246248
}
247249
}
248250

249-
private synchronized void addOrUpdate(BoardPort port) {
250-
// Update the list of discovered ports, which may involve
251-
// adding a new port, replacing the info for a previously
252-
// discovered port, or removing a port. This function
253-
// must be synchronized with listDiscoveredBoards(), to
254-
// avoid changing the list while it's being accessed by
255-
// another thread.
251+
private void addOrUpdate(BoardPort port) {
256252
String address = port.getAddress();
257253
if (address == null)
258254
return; // address required for "add" & "remove"
259255

260-
// if address already on the list, discard old info
261-
portList.removeIf(bp -> address.equals(bp.getAddress()));
262-
263-
portList.add(port);
256+
synchronized (portList) {
257+
// if address already on the list, discard old info
258+
portList.removeIf(bp -> address.equals(bp.getAddress()));
259+
portList.add(port);
260+
}
264261
}
265262

266-
private synchronized void remove(BoardPort port) {
263+
private void remove(BoardPort port) {
267264
String address = port.getAddress();
268265
if (address == null)
269266
return; // address required for "add" & "remove"
270-
portList.removeIf(bp -> address.equals(bp.getAddress()));
267+
synchronized (portList) {
268+
portList.removeIf(bp -> address.equals(bp.getAddress()));
269+
}
271270
}
272271

273272
@Override
274-
public synchronized List<BoardPort> listDiscoveredBoards() {
275-
// return the ports discovered so far. Because the list of
276-
// ports may change at any moment, a copy of the list is
277-
// returned for use by the rest of the IDE. This copy
278-
// operation must be synchronized with update() to assure
279-
// a clean copy.
280-
return new ArrayList<>(portList);
273+
public List<BoardPort> listDiscoveredBoards() {
274+
synchronized (portList) {
275+
return new ArrayList<>(portList);
276+
}
281277
}
282278

283279
@Override
284280
public List<BoardPort> listDiscoveredBoards(boolean complete) {
285281
// XXX: parameter "complete "is really needed?
286282
// should be checked on all existing discoveries
287-
return new ArrayList<>(portList);
283+
synchronized (portList) {
284+
return new ArrayList<>(portList);
285+
}
288286
}
289287

290288
@Override

0 commit comments

Comments
 (0)