Comment by paxys
It isn't really a solvable problem as long as there is no standardized/regulated tech industry. Companies want coders who will fit into the "culture" – work whatever hours they need to, be on call 24x7, chase the newest fads, accept fantasy money, "move fast and break things" etc. There is an unlimited stream of new grads who will happily accept all this. Older people meanwhile don't have the patience or have other responsibilities in life.
Go into companies and industries where this culture isn't the norm, however, and you'll find teams full of 40-50+ year old software engineers. Defense, aerospace, government, construction, medical tech, research labs, education.