comparison src/stream/ngx_stream_upstream_zone_module.c @ 7078:1eb753aa8e5e

Upstream zone: store peers->name and its data in shared memory. The shared objects should generally be allocated from shared memory. While peers->name and the data it points to allocated from cf->pool happened to work on UNIX, it broke on Windows. On UNIX this worked only because the shared memory zone for upstreams is re-created for every new configuration. But on Windows, a worker process does not inherit the address space of the master process, so the peers->name pointed to data allocated from cf->pool by the master process, and was invalid.
author Ruslan Ermilov <ru@nginx.com>
date Tue, 01 Aug 2017 19:12:10 +0300
parents 8ed51b02f655
children ac120e797d28
comparison
equal deleted inserted replaced
7077:2a288909abc6 7078:1eb753aa8e5e
180 180
181 static ngx_stream_upstream_rr_peers_t * 181 static ngx_stream_upstream_rr_peers_t *
182 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool, 182 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool,
183 ngx_stream_upstream_srv_conf_t *uscf) 183 ngx_stream_upstream_srv_conf_t *uscf)
184 { 184 {
185 ngx_str_t *name;
185 ngx_stream_upstream_rr_peer_t *peer, **peerp; 186 ngx_stream_upstream_rr_peer_t *peer, **peerp;
186 ngx_stream_upstream_rr_peers_t *peers, *backup; 187 ngx_stream_upstream_rr_peers_t *peers, *backup;
187 188
188 peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); 189 peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t));
189 if (peers == NULL) { 190 if (peers == NULL) {
190 return NULL; 191 return NULL;
191 } 192 }
192 193
193 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t)); 194 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t));
195
196 name = ngx_slab_alloc(shpool, sizeof(ngx_str_t));
197 if (name == NULL) {
198 return NULL;
199 }
200
201 name->data = ngx_slab_alloc(shpool, peers->name->len);
202 if (name->data == NULL) {
203 return NULL;
204 }
205
206 ngx_memcpy(name->data, peers->name->data, peers->name->len);
207 name->len = peers->name->len;
208
209 peers->name = name;
194 210
195 peers->shpool = shpool; 211 peers->shpool = shpool;
196 212
197 for (peerp = &peers->peer; *peerp; peerp = &peer->next) { 213 for (peerp = &peers->peer; *peerp; peerp = &peer->next) {
198 /* pool is unlocked */ 214 /* pool is unlocked */
216 return NULL; 232 return NULL;
217 } 233 }
218 234
219 ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t)); 235 ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t));
220 236
237 backup->name = name;
238
221 backup->shpool = shpool; 239 backup->shpool = shpool;
222 240
223 for (peerp = &backup->peer; *peerp; peerp = &peer->next) { 241 for (peerp = &backup->peer; *peerp; peerp = &peer->next) {
224 /* pool is unlocked */ 242 /* pool is unlocked */
225 peer = ngx_slab_calloc_locked(shpool, 243 peer = ngx_slab_calloc_locked(shpool,