That said, the general rule of contract law is that contracts (other than personal service contracts) are assignable unless the contract contains explicit language that forbids assignment without the consent of both parties.
Even then, in some jurisdictions, such as New York, a contract that purports to forbid assignment may not prevent the assignment being effective, although the assignment is a breach of the contract for which the nonassigning party may be entitled to damages. In New York, for example, to really prohibit assignment, you must specify that any attempt to assign without the requisite consent will be void ab initio. The fact that the lender has written off a defaulted loan on its books, and itself declared bankruptcy does not make the loan contract invalid, and, as a part of the process of realizing as much value for the creditors of the lender as possible, the debtor-in-possession or the trustee in bankruptcy may well sell off the portfolio of loans.
The only way the loan debt would cease to be a valid debt would be (1) if the lender had expressly forgiven the loan in writing (in which case the borrower would have taxable income in the amount of the forgiven principal and interest-warning!) or (2) if the borrower had the secured loan debt (and any deficiency judgment in states that permit them if the foreclosure sale did not yield enough to pay off the loan debt) discharged in the borrower's bankruptcy.
A third way the debt would be no longer valid would be if the lender foreclosed on the mortgage (or took a deed in lieu of foreclosure) in a state with a ‘single action’ or ‘anti-deficiency’ statute that limits the rights of a lender to recover from a homeowner to the value of the primary residence by which the loan is secured. In states that permit deficiency judgments, such as Connecticut, you would not be discharged from the difference between the loan balance (principal plus interest) and the net proceeds of the foreclosure sale.