Try a set of reins (suitable from when starting to walk 'til about 3 - when you stop depends on when they learn to stop running or wandering off!!)- not sure where you're from, but most shops like Mothercare etc sell them. I used them with my three children and it prevented then from making an escape bid. There are lots of places far more dangerous than a library where you want to prevent him from getting hurt or lost. Some people don't like them, as they think they restrict their child - duh!- that's exactly what they're meant to do, until they are a bit older and learn to stay close by. I see parents regularly running after little children who are close to roads, water or an escalator in a shop. I think I know which situation I prefer.....
Sounds like your son is enjoying himself and turning this into a game. How do you react once you have retrieved him? You must be quite firm and show him this is not a good idea. I agree with the advice already given, use reins or keep a strong hold of his hand. It is his safety at stake and you cannot afford to risk this, there should be no compromises with safety. Good luck