I've flown MUC-YYZ-SFO a few times.
Sometimes, after being at the gate, I decide to exit and have dinner with someone (in at least one case, it was a VDB that pushed my departure out by 4 hours).
They have no idea whether I've cleared Canadian immigration. In that case, I hadn't.
Another time was YYZ-YUL-SFO when YUL-SFO was cancelled. I was definitely legally in Canada, but I still had to go through, because I'd mingled with others who may not have cleared Canadian immigration.