Arduino Wire Library for Teensy: onRequest Also Triggers OnReceive

Hi everyone, I am writing Master/Slave I2C program on two Teensy boards.
The master sends some data to the slave and then requests some data from it.

I noticed that in my code, when the master requests data from slave, it also somehow triggers the onReceive event on the slave. Here is the master code:

#include <Wire.h>

void setup()
{
    Wire.begin();        
    Serial.begin(115200);
    delay(500);   
}

void loop()
{
    Serial.println("Sending");
    Wire.beginTransmission(10); 
    Wire.write("12345");               
    Wire.endTransmission();
    Serial.println("Sent");
    delay(5000);
    
    Serial.println("Requesting");
    Wire.requestFrom(10, 2);    
    while(Wire.available())
    {
        char c = Wire.read();
        Serial.print(c);
    }
    Serial.println();
    Serial.println("Requested");
    delay(5000);
}

And the code for slave:

#include <Wire.h>

void setup()
{
    Serial.begin(115200);
    delay(500);
    Wire.begin(10);                
    Wire.onRequest(requestEvents);
    Wire.onReceive(receiveEvents);
}

void loop(){}


void requestEvents()
{
    Wire.write("OK");
}

void receiveEvents(int howMany)
{  
    char c[10];
    c[0]='\0';
    int index=0;
    while(Wire.available())
    {
        c[index++]=Wire.read();
    }
    c[index]='\0';
    
    if((howMany!=5)||(strlen(c)!=5)) // Just so that there is no Serial activity
                                     // in the interrupt routine when everything is normal.
    {
        Serial.printf("wrong: %d: '%s'\r\n",howMany,c);
        
    }
}

When the code is run, I open serial monitor on both devices and I see that as soon as the master shows:

Requesting
OK
Requested

the slave shows:

wrong: 7: '1234555'

Master is a Teensy 4.1 and the slave in a Teensy 3.6.

I also checked the I2C lines on a logic analyzer and the extra send is not happening on the physical lines, so it is only something in the code for slave.

Any help is greatly appreciated.

It could be a bug of 'howMany' and 'Wire.available()'.

If 5 bytes are transmitted and 5 data bytes are shown on the logic analyzer, and then 'howMany' is suddenly 7, then something is wrong.

You can make the test cleaner.
Make an array of 5 bytes and transmit that array.

Remove everything that is not needed: The Wire.requestFrom() in the Master and the requestEvent in the Slave.

byte myData[5] = { 0x20, 0x45, 0x78, 0xAB, 0xE2 };
Wire.beginTransmission(10); 
Wire.write( myData, 5);               
Wire.endTransmission();

In the Slave, print the 'howMany' and then print the received bytes, for example as HEX numbers.

When a Wire.read() is done 7 times and there are only 5 bytes in the buffer, then it should return -1 and not the last databyte. That could be another bug.

If you start a something on a other forum or make an Issue on Github, we appreciate it if you give links over there to here and link here to there.

Thanks for your reply,
I replaced the transmission with the buffer you suggested, but nothing changed.

As to your suggestion to remove the requestFrom, actually when the master sends the data, the slave receives it correctly with no problems. The problem is when the master requests the data. After the data is requested and is sent back from slave to master, for some reason the onReceive event on the slave is triggered too. I also tested this code on Uno (with printf line replaced by the following lines) and there are no issues. So the problem must be with Teensy Wire library.

        Serial.print("wrong: ");
        Serial.print(howMany,DEC);
        Serial.print(": '");
        Serial.print(c);
        Serial.println("'");