Yes, it can be ambiguous who a pronoun refers to. Not just when using the word "while" but in many cases.
Sometimes it's obvious from the context. "While the doctor was operating on the patient, he dropped a scalpel." I assume you mean the doctor dropped a scalpel and not the patient. I expect a doctor to hold a scalpel while operating, so he might drop it. It would be unlikely for the patient to be holding a scalpel. The patient is probably unconscious from anesthetics and so not holding anything that he could drop. Etc.
Sometimes it's clear from grammatical clues, like what pronouns are used. Like, "While the mother was feeding the baby, he started choking." This must mean the baby started choking and not the mother, because if we were talking about the mother we would say "she". Note that if we said "she", it could refer to either one because the baby might be a girl.
Sometimes the writer realizes the sentence could be ambiguous and adds words to clarify. Sometimes a writer will simply add a word or two in parentheses to identify who he is talking about. Like, "Bob met George shortly after he (George) got married." "He" here could refer to either Bob or George. Unless we have other information in context to clarify which was meant, the sentence would be ambiguous.
But yeah, sometimes it's just ambiguous.
There are jokes that rely on the audience making an assumption about who is meant by a pronoun, and then the punchline is to reveal that the speaker meant a different, unlikely person.