

FD_ZERO your fd_sets or suffer
source link: http://rachelbythebay.com/w/2011/10/11/fdset/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

FD_ZERO your fd_sets or suffer
Have you ever run into this problem before? You have a select loop. It's the same old same old: keep a list of client fds somewhere, and every time through, FD_SET them into your structure. Then if they pop up as active and read fails, or if a write fails, shut them down, close them, and remove them from your list.
But... once in a while, select goes bonkers. It says you're selecting an invalid fd. Your program probably bombs on this, since select returning < 0 with errno != EINTR is not a good sign. You look and you look and you're handling everything properly! You sure don't re-add a deleted fd the next time around through the loop so what gives?
The problem is probably that you're not calling FD_ZERO on your fd_set structure every time around. Let's say you close a fd which is not the highest numbered one open, so max_fd doesn't change. Odds are, you'll start up with junk data in your fd_set which may well be the previous instance of it. It might just have the old fd's bit set, and select will go looking for it, even though you did not explicitly request it.
This seems to creep in when you get used to C++ semantics regarding objects having constructors and all of that and then you start using some good old C stuff again. You can't rely on what shows up there in your automatic variables. It might just be garbage.
Squashing that bug brings me one step closer to adding more cities to the scanner. Yay.
Recommend
-
84
Everyday I see the questions “How to work with API?”, “Can you give some best practices”, “How to cook axios” in frontend chats. They get a lot of answers but all of them can be reduced to three…
-
48
r/java: News, Technical discussions, research papers and assorted things of interest related to the Java programming languageNO programming help, NO learning Java related questions!
-
5
These Modern Programming Languages Will Make You SufferWelcome to the ultimate rating of modern programming languages
-
7
Kiss o' death: plan ahead or suffer later Wikipedia has a great article about NTP server misuse and abuse which should be required reading for anyone creating a...
-
8
Take control of your e-mail now or suffer later There are a bunch of questions that people should start asking themselves, in light of these reports that their Google accounts have been shut down for one reason or another. How mu...
-
4
Adopting TypeScript Will Make You Suffer.Have you fallen into the TypeScript hype? You definitely are not alone. TypeScript’s benefits over JavaScript are questionable. In most cases JavaScript may be a much bett...
-
14
ConversationDeep learning models are incapable of continual learning. If you teach a CNN model to recognise cat and dog and later train it incrementally to learn about horse, it's performance for cat and dog will most likely...
-
4
Google has revealed to its Google One subscribers that its High Quality photo option may reduce image quality more than previously advertised. The admission is a departure from the service's long-held position that High Quality p...
-
7
Don’t Let Legacy Code Make You Suffer. Make It Suffer Published August 4, 2021 - 0 Comments Feeling like...
-
7
California sets 2045 net-zero goal, keeps nuke plant open, and more
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK