This isn't a matter of canon law, which governs the general conduct and culpability of the people of God; this is covered by the instructions for Mass.
The last Latin Missal (the Missale Romanum of 1962) contained the following instructions. I can't find anything similar in the current General Instruction of the Roman Missal.
DE DEFECTIBUS IN CELEBRATIONE MISSÆ OCCURRENTIBUS
X - De defectibus in ministerio ipso occurrentibus
- Si, sacerdote celebrante, violetur ecclesia ante Canonem, dimittatur Missa: si post Canonem, non dimittatur. Si timeatur incursus hostium, vel alluvionis, vel ruina loci ubi celebratur, ante Consecrationem dimittatur Missa; post Consecrationem vero sacerdos accelerare poterit sumptionem Sacramenti, omissis omnibus aliis.
Si sacerdos ante Consecrationem graviter infirmetur,
vel in syncopen incident aut moriatur,
praetermittitur Missa. Si post Consecrationem Corporis
tantum, ante Consecrationem Sanguinis, vel
utroque consecrato id accidit, Missa per alium
sacerdotem expleatur ab eo loco ubi ille desiit, et
in casu necessitatis etiam per non ieiunum. Si
autem non obierit, sed fuerit infirmus, adeo tamen
ut possit communicare, et non adsit alia hostia
consecrata, sacerdos, qui Missam supplet, dividat
hostiam, et unam partem praebeat infirmo, aliam
ipse sumat. Si autem semiprolata forma Corporis
obiit sacerdos, quia non est facta Consecratio, non
est necesse ut Missa per alium suppleatur. Si vero
obierit semiprolata forma Sanguinis, tune alter
prosequatur Missam, et super eundem calicem
repetat integram formam ab eo loco: Simili modo,
postquam cenatum est; vel posset super alium
calicem praeparatum integram formam proferre,
et hostiam primi sacerdotis, et Sanguinem a se
consecratum sumere, ac deinde calicem relictum
Si quis extra huiusmodi casus necessitatis integra Sacramenta non sumpserit, gravissime peccat.
My Latin isn't great, so the following is open to correction:
OF DEFECTS OCCURRING IN THE CELEBRATION OF MASS
X - Of defects occurring in the ministry
If, while the priest is celebrating Mass, the church is violated before the Canon, the Mass is ended; if after the Canon, it is not to be discontinued. If there is fear of an attack of the enemy, or flood, or the destruction of the place where [Mass] is celebrated: before the consecration, the Mass is ended; after the consecration, the priest can speed up the reception of the Sacrament, neglecting all other things.
If before the Consecration the priest becomes seriously ill, or faints, or dies, the Mass is left undone. If after the consecration of the body only and before the consecration of the blood, or after both have been consecrated, another priest is to be sent from the place where he left off, and in case of necessity even breaking their fast. If, however, he has not died, but was ill and is still able to receive Communion, and if there is no other consecrated host, the priest who is completing the Mass divides the Host, and offers one part to the sick man, and consumes the other part himself. If the priest has died during confecting the Body, in that there is no consecration, it is not necessary for another [priest] to be supplied for the Mass. If, however, he died during the offering of the Blood, then the other [priest] continues the Mass, repeating the whole form from that place, over the cup: In a similar way, when supper was ended; or the whole formula could be offered over another prepared chalice, and the host of the first priest, and the Blood consecrated by himself, and then the cup left half-consecrated.
If anyone does not receive the whole Sacrament aside from cases of necessity of this kind, he sins gravely.
Paragraph 2 is probably the most relevant. If the priest has not yet consecrated the sacred species, the Mass is ended and he does not continue. If he has done so, then he can omit as much of the rest of the Mass is necessary in order that the sacrament can be consumed.
Archbishop Oscar Romero was murdered at the altar, but I don't know at what point that was in the Mass or what happened afterwards. I understand that Fr Jacques Hamel consumed the sacrament himself rather than let it be desecrated, before he was killed.