The home country doesn't always immediately set them free.
Depends on the country, obviously.
But the habit is that terrorist countries trade murderers and real-spies to come home, so they can set them free, and even more, encourage them to repeat the same actions (in foreign countries), but tell them to this time not get caught, if possible.
But for the normal countries, not ruled by dictator for example, these people were caught in terrorist countries for no reason at all, they are the bait of the trade, so they call them spies but they are not. So when these people come home : they didn't commit a crime in the terrorist country they were accused of, so why would they be in prison in their home country ?
If there is a trade, there is a 100% certainty something illegal is going on. In almost all cases at least one side is incorrectly accused. On global level, nobody can do anything against this.
The only option there is, is to NOT TRADE ANYBODY.
But that causes other problems, because terrorist countries will continue to capture aliens (people from other countries) and put them in prison for no reason at all. That is what they do. That's why they are terrorist countries. Some are even member of UN security council.