The “gain,” the income in these situations, comes from the forgiveness of the balance of the debt after the sale. Bankruptcy “discharges” the debt, and hence does not leave it to be “forgiven.” It would be important to file the bankruptcy before the release/forgiveness of the debt can occur.