in reply to Bad File Descriptors Gone Wild

This probably means STOCK was not opened at the time you attempted to close it. Without more information, it's impossible for us to tell you why.

Be sure that you're checking the return value from your open call so that you're sure STOCK was, in fact, opened.