If two or more sketches are open, each with a Serial Monitor, the board's output randomly goes to one, the other, or both Serial Monitors. It should go to either all Serial Monitors (they should just be displays of one underlying document), or only to the most-recently-uploaded sketch.
(This would be solved if the Serial Monitor were a separate window, as discussed extensively elsewhere.)
This is how Arduino IDE 1.x worked (though there was an unintuitive workaround), and was a frequent cause of complaints. Arduino IDE 2.x was specifically designed to allow monitoring of multiple ports via multiple Serial Monitor windows. I can't imagine why anyone would request the loss of this useful capability.
Perhaps you are not familiar with how the Arduino IDE 2.x Serial Monitor works, and this is the source of the confusion.
Serial Monitor monitors the port you have selected from the Tools > Port menu in the Arduino IDE window of that Serial Monitor. Please carefully double check which port you have selected in the IDE. If you are seeing output in the Serial Monitor from a different port than the one selected, that would indeed be a bug and one I am interested in investigating. Otherwise, Serial Monitor is working exactly as intended.
I just did a test with two Teensy boards connected to my computer and found that the Serial Monitors only showed the output from the selected port. I tried it with Teensy ports and serial ports.
Those discussions were only about the location in a dedicated window being more convenient. That is completely separate from the idea of only being able to monitor a single port. There have not been any complaints or requests about the capability of monitoring multiple ports.
I hadn't considered the use-case of two boards simultaneously. I can see why the 2.x improvements are a good solution.
But that's not what I'm doing. I have a single board and a single port, with two sketches open that alternately compile and upload. Each sketch window has an instance of a Serial Monitor. When I do this, it usually works, but sometimes the output goes to one window, the other window, or both. It's not reliably reproducible (or I haven't been able to reproduce it consistently), but it is frequent.
Thanks for the clarification. Which type of port do you have selected from the Tools > Port menu in the Arduino IDE? Depending on what you had selected from the Tools > USB Type menu, the Teensy board may produce a port of the serial protocol in addition to the "teensy" protocol HID port.
For example, here my Teensy 4.1 is producing both a serial protocol port on COM5 and a "teensy" protocol port on usb:0/1A0000/0/1/5/1/1/3:
In this example, I have selected the "teensy" protocol port.
I always select the USB serial port that has the "(Teensy 4.1)" next to it. I've never seen more than one. (See screenshot below.) I assume that's the correct one?
I don't know whether there is necessarily a "correct one" (I'm actually not very knowledgeable about Teensy unfortunately).
However, the information is potentially important due to Arduino IDE 2.x's "pluggable monitor" capability, which is used by the Teensy boards platform. When you have the "teensy" protocol port selected from the Tools > Port menu, data is provided to the Serial Monitor GUI by Paul Stoffregen's teensy-monitor tool. When you have a "serial" protocol port selected, data is provided to the Serial Monitor GUI by Arduino's serial-monitor tool.
It is possible the bug might be specific to one of those two tools. I will do some additional testing focused specifically on the teensy protocol port and report back here.
OK, I think I've got a reproducible case. I'm not surprised you couldn't reproduce it; this is probably not a normal use case. Even so, it's probably a bug of some sort.
Open two sketches, and open Serial Monitor in each (sketches that produce output).
Compile and upload one, then after it has produced output, compile and upload the other one.
At this point all is well; both Serial Monitors show the correct contents. I switched back and forth a couple times.
While one of the sketches is running, close that window.
Open a third sketch, open the Serial Monitor in the new window, compile and upload the sketch.
At this point, the output appears in the other window, and the sketch's own Serial Monitor remains blank. See screenshot below.
Actually, no screenshot! Interestingly, when I resized the Arduino window to make the screenshot smaller, all of the correct text appeared. I repeated, now here's a screenshot. Notice that the scrollbar indicates that there is some data, but it doesn't show. So maybe it's an autoscroll problem.