Serial.list() not updating correctly

I have 2 original arduino nano every boards.

If I run following code (CODE1):

import processing.serial.*;

Serial myPort;

void setup(){
}

void draw(){
  println(frameCount);  // just to see that code is running
  printArray(Serial.list());
}

Then when I connect a Arduino board, a COM port appears in the print section. And If I disconnect an Arduino board (removing usb cable) the COM port disappears in the print section.
And in windows device manager also, the board appears and disappears correctly.
Until here everything is fine.

However if I use following code (CODE2):

import processing.serial.*;

Serial myPort;

void setup(){
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw(){
  println(frameCount);  // just to see that code is running
  printArray(Serial.list());
}

In this code I am initailizing serial port.
so One board (BOARD1) has to be connected when the program starts. And after program starts, this COM port starts appearing in the print area also. (it also appears in windows device manager)
BUT IF I DISCONNECT this BOARD1 (removing the usb cable), it’s COM port doesn’t disappears from the print area. (it disappears from windows device manager)

And during this state if I connect/disconnect another board (BOARD2), then it appears and disappears perfectly in the print area.

So why the COM port I initialized, does not disappear when the cable is removed ??
What do I need to do to make it disappear, after cable is removed??

Please Help.

Hello @Ghostsss,

This may offer some insight and seems to apply to behavior in W10 also:

https://code.google.com/archive/p/java-simple-serial-connector/issues/40

You can monitor the registry:

Be sure to refresh view to see changes…

:)