[PROBLEM] Timer CTC (Compare) Interupts Triggered Early

Arduino Uno Board

In my code (simplified version for posting here) timer2 is started in CTC (compare) mode, with interrupts disabled, f=2MHz. Sending ‘a’ to the Uno sets the timer to 0 and the CTC top marker to 100, then enables the CTC interrupt. The code below measures the time between this happening and the first CTC interrupt occurring, which you would expect to be about 50us apart (100 / 2,000,000), as it will take 50us for the timer to count up to 100. However if you observe this code running in practice, this time is actually about 8us.

From my testing it appears that enabling the CTC interrupt somehow immediately triggers a CTC interrupt, regardless of the time stored by timer2.

I don’t understand why this happens, and any help is much appreciated.

uint32_t start = 0;
uint32_t finish = 0;


ISR(TIMER2_COMPB_vect)
{    
  finish = micros();
  Serial.println(finish - start);

  TIMSK2 &= ~(1 << OCIE2B);
}


void trigger()
{
  cli();

  //TCCR2A = 0;
  //TCCR2B = 0;
  //TIMSK2 = 0;

  TCNT2 = 0; // timer2 count
  OCR2B = 100; // output compare register B

  //TCCR2A |= (1 << WGM21); // set WGM21 bit so timer runs in CTC (clear timer on compare) mode
  TIMSK2 |= (1 << OCIE2B); // enable interupt for timer2
  //TCCR2B |= (1 << CS21);

  start = micros();

  sei();
}


void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  Serial.print("serial begin ");
  Serial.println(9600);

  cli();

  // reset timer2 settings
  TCCR2A = 0;
  TCCR2B = 0;
  TIMSK2 = 0;

  TCCR2A |= (1 << WGM21); // set WGM21 bit so timer runs in CTC (clear timer on compare) mode
  OCR2A = 200; // set Output Compare Register

  // set CS21 bit so timer runs at 1/8 clock speed:
  TCCR2B |= (1 << CS21);

  sei();
}


void loop() {
  // put your main code here, to run repeatedly:

  while(Serial.available()) if(Serial.read() == 'a') trigger();

  if(TCNT2 > 200) Serial.println("fail");
}

Note: disabling the timer in setup then enabling it in the "trigger" function fixes the problem and causes the correct time to be printed, however disabling the timer at the top of the "trigger" function does not.