There are at least two situations involving recourse debt in which foreclosure results in gain or loss, but not in COD income.
First, sometimes when a house is transferred to the lender by foreclosure the lender does not cancel the remaining unpaid portion of the debt. This could happen if the lender believes it can still collect the balance of the debt. In that circumstance, you would not have COD income until the lender discharged the debt or the statute of limitations on collection of the debt expired. The gain or loss on the foreclosure is the difference between the FMV of the property and its adjusted basis.
Second, sometimes the FMV of a house that is foreclosed upon is greater than the amount of the debt. If the FMV is sufficient to pay the debt in full, the debt is satisfied and there is no COD income because no part of the debt was discharged or cancelled. For example, if the FMV of the house was $200,000, the amount of the debt was $140,000, and the adjusted basis of the house was $110,000, the gain on the sale of the house is $90,000 ($200,000 FMV (amount realized) less $110,000 adjusted basis), but there is no COD income because the FMV of the house is $60,000 ($200,000 FMV less $140,000 debt), which is more than enough to satisfy the debt in full.
Source: www.irs.gov