Skip to content

Commit f4edf58

Browse files
committed
PR comments
1 parent 524ac36 commit f4edf58

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Diff for: ext/integrations/exec_integration.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct php_process_handle php_process_handle;
2121
/* popen stream handler close interception */
2222

2323
static int dd_php_stdiop_close_wrapper(php_stream *stream, int close_handle);
24-
static bool dd_waitpid(ddtrace_span_data *, php_process_id_t);
24+
static void dd_waitpid(ddtrace_span_data *, php_process_id_t);
2525

2626
ZEND_TLS HashTable *tracked_streams; // php_stream => span
2727
static zend_string *cmd_exit_code_zstr;
@@ -110,15 +110,15 @@ static int le_proc;
110110
static int le_proc_span;
111111

112112
static void dd_proc_wrapper_rsrc_dtor(zend_resource *rsrc) {
113-
// this is called from the begginning of proc_open_rsrc_dtor,
113+
// this is called from the beginning of proc_open_rsrc_dtor,
114114
// before the process is possibly reaped
115115

116116
dd_proc_span *proc_span = (dd_proc_span *)rsrc->ptr;
117117

118118
ddtrace_span_data *span_data = OBJ_SPANDATA(proc_span->span);
119119

120120
if (span_data->duration == 0) {
121-
(void)dd_waitpid(span_data, proc_span->child);
121+
dd_waitpid(span_data, proc_span->child);
122122

123123
dd_trace_stop_span_time(span_data);
124124
ddtrace_close_span_restore_stack(span_data);
@@ -128,10 +128,10 @@ static void dd_proc_wrapper_rsrc_dtor(zend_resource *rsrc) {
128128
efree(proc_span);
129129
}
130130

131-
static bool /* reaped */ dd_waitpid(ddtrace_span_data *span_data, php_process_id_t pid) {
131+
static void dd_waitpid(ddtrace_span_data *span_data, php_process_id_t pid) {
132132
if (span_data->duration) {
133133
// already closed
134-
return false;
134+
return;
135135
}
136136
zend_array *meta = ddtrace_property_array(&span_data->property_meta);
137137

@@ -146,10 +146,13 @@ static bool /* reaped */ dd_waitpid(ddtrace_span_data *span_data, php_process_id
146146
}
147147

148148
if (pid_res != pid) {
149-
return false; // some other error. Probably the process is no more/not a child
149+
return; // 0 was returned (no changed status) or some error
150+
// Probably the process is no more/not a child
150151
}
151152

153+
bool exited = false;
152154
if (WIFEXITED(wstatus)) {
155+
exited = true;
153156
wstatus = WEXITSTATUS(wstatus);
154157
} else if (WIFSIGNALED(wstatus)) {
155158
// wstatus is not modified!
@@ -160,18 +163,16 @@ static bool /* reaped */ dd_waitpid(ddtrace_span_data *span_data, php_process_id
160163
zval has_signalled_zv;
161164
ZVAL_INTERNED_STR(&has_signalled_zv, has_signalled_zstr);
162165
zend_hash_update(meta, error_message_zstr, &has_signalled_zv);
166+
exited = true;
163167
} // else !FG(pclose_wait) and it hasn't finished
164168

165-
if (wstatus != -1) { // we matched one of the two branches above
169+
if (exited) {
166170
zval zexit;
167171
ZVAL_LONG(&zexit, wstatus);
168172

169173
// set tag 'cmd.exit_code'
170174
zend_hash_update(meta, cmd_exit_code_zstr, &zexit);
171-
return true;
172175
}
173-
174-
return false;
175176
}
176177

177178
static PHP_FUNCTION(DDTrace_integrations_exec_proc_assoc_span) {

Diff for: src/Integrations/Integrations/Exec/ExecIntegration.php

+11
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ static function (HookData $hook) {
7777
);
7878

7979

80+
/*
81+
* This instrumentation works by creating a span on the enter callback, and then
82+
* associating this span with the resource returned by proc_open. This association
83+
* is done by adding a resource to the list of pipes of the proc resource. This
84+
* resource (of type dd_proc_span) is not an actual pipe, but it doesn't matter;
85+
* PHP will only ever destroy this resource.
86+
*
87+
* When the proc resource is destroyed, the dd_proc_span resource is destroyed as
88+
* well, and in the process the span is finished, unless it was finished before
89+
* in proc_get_status.
90+
*/
8091
\DDTrace\install_hook(
8192
'proc_open',
8293
static function (HookData $hook) {

0 commit comments

Comments
 (0)