The answer would depend what you mean by "legally" in/outside. The U.S., like all other countries, is fully sovereign in executing its laws over its territories (land, air, space, aircrafts, ships etc.) regardless of the legal fiction of passing through the customs and border controls, subject to its laws and international treaties it has agreed to and ratified.
Even in a country with exit control or separate areas for international departure, its law still fully applies after exit control or at the departure area, but certain legal provisions may not apply in a certain area for certain purposes (customs, taxation, VAT, etc.), only because that country's law says so.
For example, with respect to criminal offences, the Tokyo Convention limits (and extends) the exercise of criminal jurisdiction over an aircraft in flight:
Art. 1, para. 3 For the purposes of this Convention, an aircraft is considered to be in flight
from the moment when power is applied for the purpose of take-off until the moment
when the landing run ends.
Art. 4 A Contracting State which is not the State of registration may not interfere with an
aircraft in flight in order to exercise its criminal jurisdiction over an offence
committed on board except in the following cases:
- (a) the offence has effect on the territory of such State;
- (b) the offence has been committed by or against a national or permanent resident of such State;
- (c) the offence is against the security of such State;
- (d) the offence consists of a breach of any rules or regulations relating to the flight or manoeuvre of aircraft in force in such
- (e) the exercise of jurisdiction is necessary to ensure the observance of any obligation of such State under a multilateral
however, Art. 4 is a limitation of the manner of exercising a State's criminal jurisdiction over foreign vessels (you cannot interfere with operation of international flights except in limited circumstances); it does not abrogate the claimed jurisdiction itself, nor does it limit the State's authority of aircrafts registered under its laws:
Art. 3, para. 3. This Convention does not exclude any criminal jurisdiction exercised in
accordance with national law.
The United States (the federal government) claims a special aircraft jurisdiction as well as a special maritime and territorial jurisdiction of the United States in regard to various offences (e.g. offences against aircrafts, murder, certain sex offences etc.). For the purpose of the special aircraft jurisdiction which applies to certain foreign aircrafts (that have a scheduled destination or departure point in the U.S.),
An aircraft is "in flight" from the moment when all external doors are closed following embarkation until the moment when one such door is opened for disembarkation, or in the case of a forced landing, until competent authorities take responsibility for the aircraft. 49 U.S.C. § 46501(1).
So in cases where this jurisdiction could apply (or be considered to be as if it would apply), you do not leave the jurisdiction until your aircraft is ready for disembarkment. Of course, this line of argument could be a slippery slope that leads to the conclusion that you never "leave" (even if you never "entered") the U.S. (or any given country) since there are also offences that the U.S. (or another country) claims worldwide jurisdiction.
For cases related to your linked question
CBP publishes a guide on diverted flights, which states
Passengers and crew on flights departing the United States for foreign destinations that make an
unscheduled stop in the United States due to any emergency, including but not limited to fuelrelated, weather-related, mechanical-related, illness on board, or other emergency reason,
generally are not be processed by CBP, but treated as a domestic arrival.