The hadith does not say that chromosomes are assigned only after forty days, nor that Allah first learns or decides the matter then. It speaks about an angelic recording and the formation of the embryo at a stage when sexual development begins to become outwardly organized.
Why critics see a real tension
The strongest form of the objection is this: biological sex, in the ordinary chromosomal sense, is fixed at fertilization when an X- or Y-bearing sperm fertilizes the egg. So if the hadith means "the fetus is neither male nor female until day 40 or 45," it would be biologically false. Critics also object that semen or sperm cannot remain alive in the womb for forty days. Finally, if Allah already decreed everything before creation, why would an angel ask and write these matters only then?
What the hadith actually says
"When forty-two nights have passed over the drop, Allah sends an angel to it, who shapes it and makes its hearing, sight, skin, flesh and bones. Then he says: 'O Lord, male or female?' Then your Lord decrees what He wills, and the angel writes."
Sahih Muslim 2645
The wording is not, "sex is biologically created for the first time at this moment." It says an angel is sent, the embryo is shaped, and certain matters are written. In Arabic, "nutfah" is not a modern laboratory term meaning only living sperm cells. In Quranic and hadith usage it can refer to the tiny reproductive drop from which the human being begins, and by extension the earliest embryo developing from it.
So the hadith is not claiming that sperm survive for forty-two days. It is speaking from the perspective of the pregnancy: a tiny origin placed in the womb, then a stage at which shaping and recording occur.
Biologically, chromosomal sex is normally set at fertilization. But visible sexual differentiation is not obvious immediately. The development of gonads and the pathway toward male or female anatomical features becomes active in the early weeks, around the period the hadith mentions. That does not make the hadith a textbook of genetics; it does mean the wording need not contradict biology unless one forces it to mean "chromosomes are assigned only now."
Predestination and angelic writing are not the same thing
Islam teaches that Allah's knowledge and decree are eternal.
"Indeed, We created all things according to decree."
Quran 54:49
It also teaches that decrees are written and disclosed to angels at different stages. The writing in the womb is not Allah deciding after ignorance. It is an angel being commanded to record and carry out what Allah already knows and has willed.
"Then an angel is sent to him and is commanded to write four matters: his provision, his lifespan, his deeds, and whether he is wretched or blessed."
Sahih al-Bukhari 3332
This is like a royal decree being known to the king before it is delivered to an officer. The delivery is real, but it is not the beginning of the king's knowledge.
What this does and does not establish
This hadith does not prove that Islam taught modern chromosomal genetics in technical language. That would be an overclaim. It also does not say that a fetus's sex is "decided only" after forty days in the genetic sense, or that sperm remain alive for forty days.
What it does establish is a theological point: the embryo develops by Allah's command, under angelic administration, and its life is written according to divine decree. Read in its own language, the report is compatible with the distinction between chromosomal sex fixed at fertilization and later embryological formation and recording.
So the objection works only against a crude reading of the hadith. It does not show that the Prophet (PBUH) taught a biological error or contradicted the doctrine of predestination.