There are many articles on the web and answers here on StackExchange that recommend using lines like these (or even longer timeouts) to avoid errors with MySQL timeouts during long-running processes (such, but not limited to, as "MySQL server has gone away"):
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
However, I cannot find a good explanation of exactly how these solve the problems. In many cases they seem to be cargo cult programming without a clear basis. I'm curious about how precisely these settings affect the MySQL connection lifecycle: the official documentation is vague about when they apply and are measured.
From what I can tell mysql.connect_timeout only relates to the original connection to MySQL. Thus this setting seems irrelevant to connection failure issues after a successful connection has initially been made. The setting default_socket_timeout may be more relevant to later connection issues, but it's not clear exactly what would trigger the timeout. Is it also only relevant at connection, or is it a timeout for the whole socket lifetime? What triggers the timeout? Lack of traffic? Lack of a specific type of traffic?
Understanding exactly how these interact with the MySQL connection is important to be able to reason soundly about error behavior.