Aubry Johnson was criminally convicted in both state and federal court. Both courts sentenced him to serve periods *759of incarceration, with the federal sentence to run consecutively to the state sentence. While serving his state sentence, he was twice erroneously turned over to federal authorities, first from August through November of 2009 and then again from December 2009 through February 2010. Once his state sentence was complete and the Marshals Service took him into federal custody, the Bureau of Prisons (BOP) concluded that Johnson's federal sentence commenced in June 2011, when the federal government for the first time gained primary jurisdiction over him.1
Johnson filed a petition for a writ of habeas corpus challenging that determination. He argues that his federal sentence actually commenced on one of the instances when the state prematurely transferred him to the federal authorities. As a result, Johnson contends that he should receive credit against his federal sentence for the period starting on the date he was erroneously turned over to federal authorities and including all his time in state prison after he was returned to state custody. Because the state credited the time the federal authorities erroneously held Johnson against his state sentence, Johnson effectively seeks double-credit against both his state and federal sentences for the period between August 2009 and June 2011. We disagree and hold that because these erroneous transfers did not manifest the state's consent to terminate its primary jurisdiction over Johnson, he was not in federal custody for purposes of 18 U.S.C. § 3585(a), and therefore the federal sentence did not commence.
I
The Sheriff's Department in Harris County, Texas, arrested Aubry Johnson in February 2007 for fraudulently using identifying information and for violating his probation for a prior robbery conviction. In June 2007, a state court sentenced Johnson to a six-year term of imprisonment for aggravated robbery as a result of the probation violation. After sentencing, the court committed Johnson to the custody of the Texas Department of Criminal Justice (TDCJ) to serve his sentence. In August 2007, the TDCJ transferred Johnson to Fort Bend County, where a state court sentenced Johnson to a twelve-month concurrent sentence of imprisonment for fraudulent use of identifying information.
While Johnson was in state custody, the United States indicted him on federal charges for aiding and abetting device fraud and identity theft. The federal court issued writs of habeas corpus ad prosequendum for Johnson on May 10, 2007, June 29, 2007, and August 29, 2007, so that he could attend federal court proceedings.2 Upon conviction for the federal charges, the district court sentenced Johnson to an 88-month term of imprisonment, to run consecutively to his state sentence for aggravated robbery. The Marshals Service filed a federal detainer with the state *760authorities, requesting that the state hold Johnson so that federal authorities could assume custody of him when he satisfied his state sentence.3
The two errors central to this appeal occurred in late 2009. While Johnson was still serving his state sentence in the Texas prison system, the TDCJ transferred Johnson to the custody of the Dallas County Sheriff's Department to answer for additional state charges that were ultimately dismissed. Rather than return Johnson to the TDCJ, however, the Dallas County Sheriff's Department mistakenly transferred Johnson to the Marshals Service on August 7, 2009, pursuant to the federal detainer. When the error was discovered, the Marshals Service returned Johnson to the Dallas County Sheriff's Department on November 3. A short while later, on December 9, 2009, the Dallas County Sheriff's Department informed the Marshals Service that Johnson had completed his state sentence and that the department intended to release Johnson unless the Marshals Service took custody of him. On December 14, the Dallas County Sheriff's Department transferred Johnson to the Marshals Service. This was also a mistake. Johnson remained with the federal authorities until February 12, 2010, when the Marshals Service returned him to the TDCJ. Johnson received credit toward his state sentence for the periods during which the Marshals Service erroneously had physical custody of him.
Texas paroled Johnson on February 23, 2011. Because the Marshals Service had filed a federal detainer with the state, the state authorities held Johnson for federal pick-up, but due to an oversight the Marshals Service failed to retrieve him, and so Johnson was released the same day. Several months later, on June 6, 2011, Johnson visited his parole officer, at which time the Marshals Service apprehended him and turned him over to the BOP to serve his federal sentence.
The BOP determined that Johnson's federal sentence commenced on June 6, 2011, when the Marshals Service took Johnson into federal custody. Nevertheless, Johnson received credit against his federal sentence for the period during which he was released from all custody, between February 23, 2011 (when he was paroled from state custody) through June 5, 2011, when the Marshals Service apprehended him.4 Johnson objected to this calculation; he argued that his federal sentence commenced on one of the occasions when the state erroneously transferred him to the Marshals Service, either on August 7, 2009, or December 14, 2009. Therefore, Johnson contends, he is entitled to credit against his federal sentence for the time period between August 2009 and June 2011, even though the state already gave him credit for this same time period. After unsuccessfully pursuing administrative *761remedies, Johnson filed a petition for a writ of habeas corpus pursuant to 28 U.S.C. § 2241, which the district court denied. He timely appealed.
We have jurisdiction under 28 U.S.C. § 1291 and review the district court's ruling de novo. Tablada v. Thomas , 533 F.3d 800, 805 (9th Cir. 2008). Although Johnson is currently incarcerated at the Federal Correctional Institution in Oakdale, Louisiana, habeas jurisdiction was proper in the district court because Johnson filed his petition while incarcerated at the Federal Correctional Institution in Mendota, California. Brown v. United States , 610 F.2d 672, 677 (9th Cir. 1980). His subsequent transfer does not destroy the jurisdiction established at the time of filing. Francis v. Rison , 894 F.2d 353, 354 (9th Cir. 1990).
II
The federal statute governing when a term of imprisonment commences, 18 U.S.C. § 3585,5 provides that "[a] sentence to a term of imprisonment commences on the date the defendant is received in custody awaiting transportation to, or arrives voluntarily to commence service of sentence at, the official detention facility at which the sentence is to be served." 18 U.S.C. § 3585(a). In order to determine whether Johnson's federal sentence commenced when the state mistakenly transferred him to the federal government, we begin by interpreting § 3585(a) in its historical context.
A
Although "custody" can mean mere physical possession or control of a person, it may also refer to lawful authority over a person. See Black's Law Dictionary 441 (9th ed. 2009) (defining "constructive custody" as "[c]ustody of a person (such as a parolee or probationer) whose freedom is controlled by legal authority but who is not under direct physical control"); Webster's Third New International Dictionary 559 (2002) ("[C]ontrol of a thing or person with such actual or constructive possession as fulfills the purpose of the law or duty requiring it."). Courts have long interpreted "custody" in the context of § 3585 and its predecessors as referring to the federal government's control over a prisoner when it has both physical custody and primary jurisdiction.
The concept of primary jurisdiction was established by the Supreme Court nearly a century ago, when it acknowledged the need for comity between state and federal authorities with respect to managing defendants who are subject to both state and federal criminal prosecutions and sentences. See Ponzi v. Fessenden , 258 U.S. 254, 259, 42 S.Ct. 309, 66 L.Ed. 607 (1922). In Ponzi , the Supreme Court stated the general rule that the first sovereign to arrest a defendant obtains primary jurisdiction over him as against other sovereigns.
*762Id . at 260, 42 S.Ct. 309 ("The chief rule which preserves our two systems of courts from actual conflict of jurisdiction is that the court which first takes the subject-matter of the litigation into its control, whether this be person or property, must be permitted to exhaust its remedy, to attain which it assumed control, before the other court shall attempt to take it for its purpose."). Nevertheless, the sovereign with primary jurisdiction could consent to the defendant's transfer to another sovereign for trial or other proceedings. Id . at 261, 42 S.Ct. 309. Such a decision is vested "solely to the discretion of the sovereignty making it," acting through "its representatives with power to grant it." Id. at 260, 42 S.Ct. 309. In the federal system, for example, a "transfer of a federal prisoner to a state court for such purposes" may be "exercised with the consent of the Attorney General." Id. at 261-62, 42 S.Ct. 309.
Congress enacted the earliest predecessor of § 3585, 18 U.S.C. § 709a, in 1932.6 See Jonah R. v. Carmona , 446 F.3d 1000, 1003 (9th Cir. 2006) (discussing the history of § 3585 ). Courts interpreted § 709a in light of Ponzi and the concept of primary jurisdiction, concluding that a state's transfer of a defendant to the federal government does not trigger the commencement of the federal sentence unless the federal government obtains primary jurisdiction over the defendant. In Zerbst v. McPike , for instance, Louisiana state authorities had primary jurisdiction over a defendant, but transferred him to the federal government for the duration of a federal prosecution. 97 F.2d 253, 254 (5th Cir. 1938). When the federal sentencing was complete, the prisoner was returned to the state, which took him back to state jail and tried and sentenced him for a state crime. Id . After the defendant served his state sentence, he argued that his federal sentence began running when he was taken to the state jail following his federal sentencing. Id. The Fifth Circuit rejected this argument. It explained that the state had primary jurisdiction over the defendant and merely lent the prisoner to the federal government "without a complete surrender of the prior jurisdiction over him which the State had acquired." Id . Therefore, the federal sentence did not "commence" until the defendant was received at the federal penitentiary after the state sentence was complete.7 Id.
Courts interpreted 18 U.S.C. § 3568,8 the successor statute to § 709a, in light of *763this doctrine of primary jurisdiction. See, e.g. , Hayward v. Looney , 246 F.2d 56, 58 (10th Cir. 1957) (interpreting 18 U.S.C. § 3568, a recodification of 709a); United States ex rel. Moses v. Kipp , 232 F.2d 147, 150 (7th Cir. 1956) (same). In doing so, courts consistently concluded that a federal sentence did not commence until the federal government had "legal custody" of a defendant, meaning the primary jurisdiction necessary to enforce the federal sentence. Burge v. United States , 332 F.2d 171, 175 (8th Cir. 1964) ; see also Crawford v. Jackson , 589 F.2d 693, 695 (D.C. Cir. 1978). When § 3568 was recodified as § 3585, our current statute, in 1984,9 courts retained the same interpretation. See, e.g. , Elwell v. Fisher , 716 F.3d 477, 481 (8th Cir. 2013) ("Pursuant to the doctrine of primary jurisdiction, service of a federal sentence generally commences when the United States takes primary jurisdiction and a prisoner is presented to serve his federal sentence, not when the United States merely takes physical custody of a prisoner who is subject to another sovereign's primary jurisdiction."); United States v. Evans , 159 F.3d 908, 911-12 (4th Cir. 1998) (same). We have implicitly reached the same conclusion. See Taylor v. Reno , 164 F.3d 440 (9th Cir. 1998). In Taylor , the federal government surrendered its primary jurisdiction over a federal defendant by releasing him on his own recognizance pending sentencing. Id. at 443. While at large, he was arrested by the state and jailed on a murder charge. Id. State officials later produced the defendant for federal sentencing pursuant to a writ of habeas corpus ad prosequendum. Id. At his federal sentencing, the district court stated that the defendant was "now in federal custody," id ., but federal officials returned him to state custody to serve his sentence. Id . at 444. We rejected the defendant's argument that his federal sentence commenced on the date of his federal sentencing. See id . Because the defendant was in federal custody only by the state's agreement, the state maintained its priority, and "the district court did not have authority to order [the defendant] into federal custody to commence his federal sentence." Id .
Absent a clear indication to the contrary, we assume that Congress was aware that courts interpreted the predecessors to § 3585 in light of the primary jurisdiction doctrine and intended to carry that doctrine forward in enacting the materially similar § 3585. Cf., e.g. , Tex. Dep't of Hous. & Cmty. Affairs v. Inclusive Cmtys. Project, Inc. , --- U.S. ----, 135 S.Ct. 2507, 2519-20, 192 L.Ed.2d 514 (2015) (reasoning that Congress can be understood to acquiesce to widespread views in the courts of appeal); United States v. Wilson , 503 U.S. 329, 336, 112 S.Ct. 1351, 117 L.Ed.2d 593 (1992) (interpreting *764§ 3585(b) and reasoning that courts should not lightly assume "that Congress intended to depart from a long established policy" (internal quotation marks omitted) (quoting Robertson v. R.R. Labor Bd. , 268 U.S. 619, 627, 45 S.Ct. 621, 69 L.Ed. 1119 (1925) ) ). Consistent with our implicit conclusion in Taylor , and with the many decades of judicial interpretation of § 3585 and its predecessors, we therefore interpret "custody" in § 3585(a) as "legal custody," meaning that the federal government has both physical custody of the defendant and the primary jurisdiction necessary to enforce the federal sentence. Accordingly, under § 3585(a), "[a] sentence to a term of imprisonment commences on the date" that the federal government has primary jurisdiction over a defendant who is "received in custody awaiting transportation to" the official detention facility.
Our interpretation is also consistent with the BOP's understanding of the statutory scheme, to which we ordinarily afford "substantial deference." Jonah R. , 446 F.3d at 1006 ; see also Reno v. Koray , 515 U.S. 50, 61, 115 S.Ct. 2021, 132 L.Ed.2d 46 (1995) (holding that courts may defer to BOP program statements). Pursuant to a BOP Program Statement, "[w]hen it has been determined [that] an inmate was committed improperly to federal custody and primary jurisdiction resides with a state sovereign (i.e., the inmate was under jurisdiction of the federal sentencing court on the basis of a writ of habeas corpus ad prosequendum), [the BOP] will make every effort to return the inmate to state custody." U.S. Dep't of Justice, Fed. Bureau of Prisons, Program Statement No. 5160.05: Designation of State Institution for Service of Federal Sentence 11 (2003) ("Program Statement"). In such situations, the BOP's Program Statement provides that "[a] return to the state means that the federal sentence should be considered as not having commenced since transfer to the Bureau was in error and the prisoner should have been returned to the state." Id. at 12. Although the Program Statement refers to the situation in which a prisoner's erroneous federal custody is pursuant to a writ of habeas corpus ad prosequendum, see id . 11-12, the BOP's policy with regard to such writs recognizes that a federal sentence does not commence merely because a prisoner is in the federal government's physical custody. Because the BOP's interpretation is a permissible construction of the statute, we defer to it. See Reno , 515 U.S. at 61, 115 S.Ct. 2021.10
B
Having determined that a federal sentence commences only when the federal government has physical possession of and primary jurisdiction over the defendant, we must next determine when the federal government obtains such primary jurisdiction. It is well established that if a sovereign takes a defendant into its custody before another sovereign has done so, then the arresting sovereign establishes its primary jurisdiction and may *765give effect to its sentence before other sovereigns may do so. Thomas v. Brewer , 923 F.2d 1361, 1365 (9th Cir. 1991). A sovereign's priority terminates when the sentence expires, charges are dismissed, or the prisoner is allowed to go free. See Elwell , 716 F.3d at 481 ; Taylor , 164 F.3d at 445 ; cf. Strand v. Schmittroth , 251 F.2d 590, 599 (9th Cir. 1957) (en banc) ("When a defendant or a parolee or a probationer is released from actual physical custody, even for temporary purposes, he may be arrested, tried and convicted by any other such sovereign in the territory in which he may be without the consent of the first sovereign, which may have a judgment against him as yet unsatisfied or which may be seeking to try him.").
The more difficult situation arises when one sovereign transfers a defendant to another sovereign. Such a case requires an exercise of comity between the sovereigns, and turns on whether the state with primary jurisdiction intended to surrender its priority upon transfer or merely transferred temporary control of the defendant to the federal government. See United States v. Warren , 610 F.2d 680, 685 (9th Cir. 1980) (a sovereign with priority "may elect under the doctrine of comity to relinquish" control of a defendant); see also Ponzi , 258 U.S. at 266, 42 S.Ct. 309 (stating that the Attorney General may give "the consent of the United States" to permit a federal prisoner to be tried in a state's courts, but this consent does not relinquish priority). Because a state's transfer of temporary control of the defendant "extends no further than it is intended to extend," Zerbst , 97 F.2d at 254, and a state that mistakenly transferred a prisoner to the federal government lacked the intent to surrender primary jurisdiction, such a mistaken transfer does not constitute a relinquishment of primary jurisdiction. If the state retains primary jurisdiction, the federal sentence does not commence pursuant to § 3585. Therefore, a prisoner's federal sentence does not commence when the state mistakenly transfers a prisoner to the federal government.11
This conclusion raises a second question: how to determine whether the state's transfer of a prisoner is a mistake. In determining whether a state's transfer of a defendant to a second sovereign is intended to be "a complete surrender of the prior jurisdiction" that the state acquired over the defendant, Zerbst , 97 F.2d at 254, we consider the record as a whole. In light of the obligations of comity, we give particular weight to the state's own determination that the transfer of the prisoner to the federal government was a mistake. See Ponzi , 258 U.S. at 260, 42 S.Ct. 309. If the state is silent on this issue, we may consider whether the state and federal government made a formal temporary transfer of physical control pursuant to a writ of habeas corpus ad prosequendum or written request for temporary custody, see Taylor , 164 F.3d at 445, and whether a properly authorized representative of the state approved the transfer, see *766Ponzi , 258 U.S. at 260, 42 S.Ct. 309. Because the "[d]etermination of priority of custody and service of sentence between state and federal sovereigns is a matter of comity to be resolved by the executive branches of the two sovereigns," Warren , 610 F.2d at 684, two sovereigns are not bound "by the actions of mere subordinate administrative officials such as the state sheriff and federal marshal," Smith v. Swope , 91 F.2d 260, 262 (9th Cir. 1937).
The Tenth Circuit's decision in Weekes v. Fleming , 301 F.3d 1175 (10th Cir. 2002), illustrates such a record-specific analysis. In that case, a state arrested a defendant; transferred him to federal authorities for criminal proceedings in federal court; obtained his return to state court on a writ of habeas corpus ad prosequendum where he was sentenced to a term of imprisonment; and finally returned him to federal court where he pleaded guilty and was sentenced to imprisonment. Id . at 1177. After the federal authorities transferred the defendant to federal prison, the BOP determined that the defendant had not yet served his prior state sentence and returned him to state prison. Id . at 1177-78. Upon the conclusion of his state term of imprisonment and his return to federal prison, the defendant claimed that his federal sentence began when he was first transported to the federal prison. Id . at 1179.
The Tenth Circuit agreed, holding that the record demonstrated that the state had intentionally relinquished primary jurisdiction over the defendant. Id . at 1181. In determining the state's intent, the court first noted that the United States had not presented "either a written request for temporary custody or a writ of habeas corpus ad prosequendum" when it took the defendant away from state authorities, which gave rise to a presumption that both the federal government and the state government had "agreed to a permanent change of custody." Id . Further, the state's subsequent acts confirmed this presumption was correct. These acts included "(1) the subsequent use of an ad prosequendum writ to regain custody, (2) a sentencing order expressly providing that the state sentence should be served concurrently with a future federal sentence, and (3) a state-lodged detainer requesting [the defendant's] return to the state prison system upon completion of his federal sentence." Binford v. United States , 436 F.3d 1252, 1255 (10th Cir. 2006) (discussing Weekes ) (emphasis omitted). Because the record demonstrated that the state had agreed to surrender primary jurisdiction over the defendant and that "[t]he United States was under no duty to return [the defendant] to state custody after federal sentencing," the court concluded that he "must be given federal credit for time served since ... the date his federal sentence actually commenced." Id . (first and third alterations in original) (quoting Weekes , 301 F.3d at 1181 ).
The dissent argues that our conclusion may prevent a prisoner from being given credit for all time served in official custody. Dissent at 771-72. It therefore urges the adoption of a rule that the state must be deemed to have surrendered its primary jurisdiction when it transfers the prisoner to the federal government unless the state expressly preserves its primary jurisdiction through a writ of habeas corpus ad prosequendum. Dissent at 772-73. We disagree. Such an approach is contrary to the principles of comity expressed in Ponzi , which establish that the sovereign which is first to arrest a defendant obtains primary jurisdiction over him as against other sovereigns, and any transfer must be with that sovereign's consent. 258 U.S. at 260, 42 S.Ct. 309. We would interfere with the comity necessary for managing defendants who are subject to criminal prosecution *767and sentences by both state and federal sovereigns by adopting a rule that prevents sovereigns from rectifying a mistaken transfer or by holding as a matter of law that the state surrendered its primary jurisdiction when it merely made a mistake. For instance, a rule that a state's mistaken transfer of a prisoner triggers the commencement of a federal sentence might motivate federal authorities to retain such a prisoner against the wishes of the state, so as to ensure that the prisoner serves the full sentence imposed by federal law.
Moreover, the dissent's concern that prisoners will not be fully credited for time served is misplaced. Dissent at 771-72. In this case, for instance, Johnson received credit against his state sentence for time erroneously spent in federal custody. Even the dissent agrees that Johnson is not entitled to receiving credit against both his state and federal sentence for the time spent in federal custody, the result he seeks on appeal. See Dissent at 775. Nor does our interpretation of § 3585 preclude courts from fashioning remedies "to prevent the government from abusing its coercive power to imprison a person by artificially extending the duration of his sentence through releases and re-incarcerations," Free , 333 F.3d at 554, where necessary to ensure that the prisoner's period of incarceration is not extended due to a mistaken transfer.12
III
We now consider whether, under § 3585(a), Johnson's federal sentence commenced on June 6, 2011, or on one of the two occasions when the state erroneously transferred him to the Marshals Service on August 7, 2009, or December 14, 2009. The parties do not dispute that Texas was the first sovereign to obtain jurisdiction over Johnson when the state arrested him in February 2007, and Texas therefore had initial primary jurisdiction. See Thomas , 923 F.2d at 1365 (citing Warren , 610 F.2d at 684-85 ). Because Johnson's consecutive federal sentence could not commence under § 3585(a) until the federal government obtained primary jurisdiction over him, we must decide whether and when Texas relinquished its primary jurisdiction to the federal government.
Johnson argues that the record establishes that Texas relinquished its primary jurisdiction in 2009 when the Dallas County Sheriff's Department twice transferred him to the federal government and represented on one occasion that his the state sentence was complete. Moreover, as in Weekes , Johnson's transfer to federal control was not pursuant to a writ of habeas corpus ad prosequendum or a written request for temporary custody from the federal government. Thus, in Johnson's view the federal government had legal custody over him upon his erroneous transfer.
*768We disagree. As explained above, the crucial question is whether, in view of the record as a whole, the state intended to relinquish its primary jurisdiction over Johnson on August 7, 2009, or December 14, 2009, when it transferred him to the Marshals Service. Here, Johnson does not dispute that the Sheriff's Department made a mistake. Highlighting this fact, the Marshals Service's returned Johnson to state authorities when the error was discovered, and Texas took him back. By acknowledging and correcting the error, the state and federal sovereigns made clear that they had not reached an agreement to transfer primary jurisdiction over Johnson.13 Cf. Zerbst , 97 F.2d at 254 ("The prior right acquired by first arrest continues unchanged until the arresting government has completed the exercise of its powers, and a waiver extends no further than it is intended to extend." (emphasis added) ).
Johnson argues that we should follow Weekes and hold that the state intended to relinquish primary jurisdiction because the state did not transfer him to the federal government pursuant to a writ of habeas corpus ad prosequendum or a written request for temporary custody. Again we disagree. In Weekes , the absence of a writ of habeas corpus ad prosequendum was only one relevant factor, and "the further acts of the two sovereigns" confirmed the court's conclusion that the state and federal sovereigns had reached an agreement for a transfer of primary jurisdiction. 301 F.3d at 1181. Here, unlike in Weekes , there is no indication (1) that either sovereign believed that Texas would have to "borrow" Johnson by means of a writ of habeas corpus ad prosequendum in order to get physical custody, (2) that Texas consented to Johnson's serving his state sentence concurrently with his federal sentence, or (3) that Texas lodged a detainer with the federal authorities acknowledging the federal government's priority.14 See id . at 1181. Rather, the record best reflects a mutual understanding between the sovereigns that Texas's error was not a surrender of priority and that comity counseled in favor of returning Johnson to the state authorities.
We conclude that on this record, Texas established its priority of jurisdiction when it arrested Johnson in February 2007. From the time of arrest through the time Texas paroled Johnson, the state did not manifest an intent to surrender its priority in favor of the federal government. The Sheriff Department's transfers of Johnson to the federal government in August and December of 2009 were merely mistakes. Therefore, the federal government did not obtain legal custody, i.e., "custody enabling and entitling it to enforce the [consecutive federal] sentence," Burge , 332 F.2d at 175, until after Johnson completed his state sentence. The BOP accordingly did not err in determining that Johnson's federal sentence commenced on June 6, 2011, when the federal government for the first time *769exercised exclusive penal custody over Johnson.
AFFIRMED.