Arduino Mega freezes if receiving something through Serial (RX0), while booting

Today I was playing around with making Arduinos talk to each other and I noticed the following strange behavior (bug?):

If an Arduino Mega, receives a stream of bytes through Serial0, while it is booting (after being plugged into power or a reset), then the sketch never gets executed properly and the Arduino freezes! This was witnessed in both the Mega’s that I own. Additionally, this behavior was not noticeable in Arduino UNOs, or the other hardware (or software) serials of the Mega.

To recreate this issue you will need two Arduinos, one that transmits and the Mega that receives.

Transmitter Receiver (Arduino Mega)
TX <—> RX
GND <----> GND

Transmitter’s sketch

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("hi");
  delay(500);
}

Receiver’s sketch (Arduino Mega)

void setup() {
  Serial.begin(9600);
  Serial.println("Start");
}

void loop() {
  Serial.println("Foo");
}

The receiver, when it is an Arduino Mega, does not print anything when the transmitter is connected to its Serial0. It is pretty obvious that something goes very wrong.
If however, the transmitter is connected AFTER the Arduino Mega has booted up (or to a different hardware serial), then the sketch is executed normally and we can even read the incoming stream if needed. This behavior is not noticeable on the Uno.

Any ideas why this happens?!

Since it's happening on the hardware serial port that's used for uploading sketches, do you think it's possible that maybe the bootloader is causing this behaviour?

OldSteve:
Since it's happening on the hardware serial port that's used for uploading sketches, do you think it's possible that maybe the bootloader is causing this behaviour?

That is possible, however I have no way of verifying it. :confused:
What confuses me more is, if it is a bootloader issue, could the Uno bootloader be so drastically different for this issue to not be present there?

kuruki:
That is possible, however I have no way of verifying it. :confused:
What confuses me more is, if it is a bootloader issue, could the Uno bootloader be so drastically different for this issue to not be present there?

I don't know. It just struck me that since the problem occurs during bootup, the bootloader may be to blame.
I know very little about the bootloader operation, but hopefully someone more knowledgable on this subject could confirm or deny the possibility.

I guess I should open an issue on GitHub. This is very strange. Can anyone try to reproduce the problem?

could the Uno bootloader be so drastically different for this issue to not be present there?

Yes. Different bootloader, different communications protocol.
Do you know what's in your serial stream? "!!!" is known to be particularly dangerous, since it causes the bootloader to enter a "debug monitor."

westfw:
Yes. Different bootloader, different communications protocol.
Do you know what's in your serial stream? "!!!" is known to be particularly dangerous, since it causes the bootloader to enter a "debug monitor."

Oh don't remind me the "!!!", I was really surprised when I found out about this. However no, the incoming streams I have tried are so far anything other than "!" (I.e. "hi" or numbers).