Mass incarceration overall hurts the health of Americans, leading to worse outcomes for the families and communities of men in prison. The inmates themselves are at a very large risk of self-harm and violence immediately after their release. But a recent review of the impacts of incarceration on health published Thursday in The Lancet hints at a surprising upshot: Getting out of jail can be miserable, but going to jail can temporarily protect health—at least for some men.

For children and communities, the impacts of a parent’s incarceration are unequivocally bad, write study authors Christopher Wildeman of Cornell University and Emily Wang of Yale. Kids whose fathers go to jail are at increased risk of depression, anxiety, learning disabilities, and obesity, and they are more likely to do drugs later in life. Because criminal records dampen job opportunities, according to some studies people who live in neighborhoods with high levels of incarceration are more likely to experience asthma from dilapidated housing. These consequences are especially severe for children of color: Because black men are jailed disproportionately, a black child born in 1990 had a one-in-four chance of having their father imprisoned, Wildeman and Wong write.

When imprisoned fathers return home, “they have trouble finding employment,” says Kristin Turney, a sociologist at the University of California, Irvine, who has studied the health of inmates’ children but was not involved with the study. Part of the explanation is reduced income, she said, and “part of it is the relationship between the parents. Maintaining romantic partnerships while incarcerated is tricky, so it can lead to more [familial] conflict.”

But, paradoxically, going to prison can actually improve health—at least temporarily—for some inmates. Black male inmates, the authors write, have a lower mortality rate than similarly aged black men who aren’t in jail. The reason? The risk of death from violent accidents, overdoses on drugs or alcohol, and homicides is much lower in prison than it is in the neighborhoods where these men would be living otherwise. What’s more, before the Affordable Care Act was passed, many states made it all but impossible for low-income, childless men to obtain health care. Under the ACA, 32 states expanded Medicaid to cover all poor adults, but 19 have not. Because of that, Wildeman and Wang write, prison is the first time many incarcerated young men receive regular health care.

The drop in mortality “is just an indicator of how dangerous the environment for African-Americans is on the outside, rather than being a function of how good the medical care is that they’re receiving” in prison, Wildeman told me. (This health boost excludes the effect of solitary confinement, which has well-known, horrific consequences for mental health.)

Upon release, however, former inmates are more likely to have infectious diseases like hepatitis and tuberculosis—conditions they likely contracted in prison but which did not manifest until later. A previous study found that, within the first two weeks of release from prison, former inmates are also 12.7 times more likely to die than others in their area, mostly from drug overdoses, heart attacks, homicides, and suicides.

“These highly preventable causes are indicators of a tumultuous readjustment to society,” Wildeman said. People with criminal records find it much more difficult to find jobs, and the economic stress, combined with disruptions to their family lives, can take a steep toll. Addicts who go to prison typically detox without any further addiction treatment, so if they resume using upon release, their tolerance is lower, and they risk overdosing.

This does not, of course, mean that prison is some sort of healthy oasis. Most prison terms last only a few years, but the economic and psychological consequences endure for a lifetime. But it does speak to the precarious health environment for lower-income black men if prison, for some, is better than nothing.