[ZBX-10031] On Windows some thread local variables are actually not thread local Created: 2015 Oct 30  Updated: 2017 May 30  Resolved: 2015 Nov 24

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Agent (G)
Affects Version/s: 2.4.6
Fix Version/s: 3.0.0alpha5

Type: Incident report Priority: Trivial
Reporter: Sandis Neilands (Inactive) Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: codequality, windows
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2008



 Description   

See sub-issue (123) in ZBXNEXT-1263.

This is a regression since ZBX-8796, ZBXNEXT-101. process_type, process_num, server_num are NOT thread local variables.

src/zabbix_agent/zabbix_agentd.c

ZBX_THREAD_LOCAL unsigned char  process_type    = 255;  /* ZBX_PROCESS_TYPE_UNKNOWN */
ZBX_THREAD_LOCAL int            process_num;
ZBX_THREAD_LOCAL int            server_num      = 0;

src/zabbix_agent/listener.c, etc.

extern unsigned char    process_type, program_type;
extern int              server_num, process_num;

According to Microsoft this should produce (compile time?) errors. We don't have neither errors, nor warnings in our build environment regarding this problem.

You must use the thread attribute for the declaration and the definition of thread local data,
regardless of whether the declaration and definition occur in the same file or separate files.
For example, the following code generates an error: 

#define Thread   __declspec( thread )
extern int tls_i;     /* This generates an error, because the   */
int Thread tls_i;     /* declaration and the definition differ. */

This causes listener threads reusing the same number due to race condition during thread initialization when agent is started.



 Comments   
Comment by Sandis Neilands (Inactive) [ 2015 Nov 20 ]

Since these variables are used also in server it is a bit difficult to check if all relevant places were modified. One method to do that is:

  1. rename suspect variables;
  2. try to build the agentd on Windows - the linker will complain when it cannot find such symbols;
  3. add the ZBX_THREAD_LOCAL to the relevant externs;
  4. rename the variables back to their original names.

Testing: print addresses of these variables from the relevant files. Before changes they are the same across all threads. After changes - these are thread local variables with different addresses.

Tested on Windows 8.1 64bit with build targeting Windows Server 2008 64bit.

Fixed in development branch svn://svn.zabbix.com/branches/dev/ZBX-10031 .

Comment by Andris Zeila [ 2015 Nov 23 ]

Successfully tested

Comment by Sandis Neilands (Inactive) [ 2015 Nov 23 ]

Released in:

  • pre-3.0.0alpha5 r56849.
Generated at Fri Apr 26 03:08:13 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.