This is correct according to the spec. The reason the final ** can’t end strong emphasis is that it’s not part of a right-flanking delimiter run. Indeed, this run ** between _ and is is left-flanking.
I agree that this is an odd result. Especially because
***th***is does work
The two cases are different because in _** you have two distinct delimiter runs, while in *** you just have one (which is both left- and right-flanking).
Perhaps there is a way to do better on this case. However, it’s worth saying that every Markdown implementation has weird edge cases on nested strong/emph. We’ve worked very hard to specify their behavior in CommonMark in a way that gets the intuitively intended results in almost every case. But getting 100% might be impossible without building AI into the spec.
On further reflection, I think there’s a simple fix for this problem. We need only change the definition of “delimiter run” in the spec from
A delimiter run is either a sequence of one or more * characters that is not preceded or followed by a * character, or a sequence of one or more _ characters that is not preceded or followed by a _ character.
to
A delimiter run is either a sequence of one or more * or _ characters that is not preceded or followed by a * or _ character.
I will try to update the parsers accordingly and see how things go.
I’ve explored some options, and I think it’s probably best to leave things as they are.
I tried changing the definition of delimiter run as suggested above, but this led to some unwanted consequences: for example, _a_*b and _a_\*b came to have distinct meanings (only the second involved emphasis).