But in big companies (Amazon, Google, Twitter) in enterprise software companies, banks, medical tech, finance, I think it's not that uncommon to find 40 year old developers. Most of the developers in my company average at way above 30. And we have a couple of engineers above 50. All of them are pretty kick ass Java developers and they all get tons of unsolicited recruiter spam. (Maybe I'm delusional or in denial, or perhaps this is just the difference between industries, but I think that if you WANT to still do it while you are 40, and you keep yourself sharp, just like doctors have to keep up every year with the latest developments in medical research to stay relevant, then I think you can do it till you retire)
I think the reason many drop out of coding is that it is very time consuming to keep up, and some may want sometimes a less mentally demanding daily routine. Managing a team of engineers can be tough, but it's somewhat less mentally and intellectually strenuous and energy hungry as coding. Coding takes a lot of focus and sharpness, and sometimes it's simply hard grunt work, people sometimes get tired of it and want to move on to management (or being a "solution architect") or even product management.
I think the problem is that the 40 year olds chose on their own to do less coding at some point, and more management / IT / architecture. (e.g. the less good developers, those who don't pass the sort from 100 to 1 but still manage to land a job, eventually understand it's not for them and move on to other, not less important roles)