Skip to content

Commit c1a4d93

Browse files
committed
Add warning about locally closed resources being undefined behavior
Also makes Loop docs consistent with Driver. Resolves #106.
1 parent 11c04e9 commit c1a4d93

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

src/Loop.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,18 @@ public static function repeat($interval, callable $callback, $data = null)
178178
/**
179179
* Execute a callback when a stream resource becomes readable or is closed for reading.
180180
*
181+
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
182+
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
183+
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
184+
* resources are therefore undefined behavior.
185+
*
186+
* Multiple watchers on the same stream may be executed in any order.
187+
*
181188
* @param resource $stream The stream to monitor.
182189
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
183190
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
184191
*
185-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
192+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
186193
*/
187194
public static function onReadable($stream, callable $callback, $data = null)
188195
{
@@ -193,11 +200,18 @@ public static function onReadable($stream, callable $callback, $data = null)
193200
/**
194201
* Execute a callback when a stream resource becomes writable or is closed for writing.
195202
*
203+
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
204+
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
205+
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
206+
* resources are therefore undefined behavior.
207+
*
208+
* Multiple watchers on the same stream may be executed in any order.
209+
*
196210
* @param resource $stream The stream to monitor.
197211
* @param callable(string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
198212
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
199213
*
200-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
214+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
201215
*/
202216
public static function onWritable($stream, callable $callback, $data = null)
203217
{
@@ -208,16 +222,19 @@ public static function onWritable($stream, callable $callback, $data = null)
208222
/**
209223
* Execute a callback when a signal is received.
210224
*
211-
* WARNING: Installing a handler on the same signal on different scopes of event loop execution is
212-
* undefined behavior and may break things arbitrarily.
225+
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
226+
* Implementations may try to detect this, if possible, but are not required to. This is due to technical
227+
* limitations of the signals being registered globally per process.
228+
*
229+
* Multiple watchers on the same signal may be executed in any order.
213230
*
214231
* @param int $signo The signal number to monitor.
215232
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
216-
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
233+
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
217234
*
218-
* @return string An identifier that can be used to cancel, enable or disable the watcher.
235+
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
219236
*
220-
* @throws UnsupportedFeatureException Thrown if signal handling is not supported.
237+
* @throws UnsupportedFeatureException If signal handling is not supported.
221238
*/
222239
public static function onSignal($signo, callable $callback, $data = null)
223240
{
@@ -228,11 +245,15 @@ public static function onSignal($signo, callable $callback, $data = null)
228245
/**
229246
* Enable a watcher.
230247
*
248+
* Watchers (enabling or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks
249+
* can be called) right before the next tick. Callbacks of watchers MUST not be called in the tick they were
250+
* enabled.
251+
*
231252
* @param string $watcherId The watcher identifier.
232253
*
233254
* @return void
234255
*
235-
* @throws InvalidWatcherException Thrown if the watcher identifier is invalid.
256+
* @throws InvalidWatcherException If the watcher identifier is invalid.
236257
*/
237258
public static function enable($watcherId)
238259
{
@@ -243,6 +264,9 @@ public static function enable($watcherId)
243264
/**
244265
* Disable a watcher.
245266
*
267+
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
268+
* invalid watcher.
269+
*
246270
* @param string $watcherId The watcher identifier.
247271
*
248272
* @return void
@@ -256,8 +280,8 @@ public static function disable($watcherId)
256280
/**
257281
* Cancel a watcher.
258282
*
259-
* This will detatch the event loop from all resources that are associated to the watcher. After this
260-
* operation the watcher is permanently invalid.
283+
* This will detatch the event loop from all resources that are associated to the watcher. After this operation the
284+
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
261285
*
262286
* @param string $watcherId The watcher identifier.
263287
*
@@ -272,8 +296,8 @@ public static function cancel($watcherId)
272296
/**
273297
* Reference a watcher.
274298
*
275-
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state
276-
* by default.
299+
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
300+
* default.
277301
*
278302
* @param string $watcherId The watcher identifier.
279303
*
@@ -290,8 +314,8 @@ public static function reference($watcherId)
290314
/**
291315
* Unreference a watcher.
292316
*
293-
* The event loop should exit the run method when only unreferenced watchers are still being monitored.
294-
* Watchers are all referenced by default.
317+
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
318+
* are all referenced by default.
295319
*
296320
* @param string $watcherId The watcher identifier.
297321
*
@@ -309,8 +333,7 @@ public static function unreference($watcherId)
309333
* Stores information in the loop bound registry.
310334
*
311335
* This can be used to store loop bound information. Stored information is package private. Packages MUST NOT
312-
* retrieve the stored state of other packages. Packages MUST use the following prefix to keys:
313-
* `vendor.package.`
336+
* retrieve the stored state of other packages. Packages MUST use the following prefix for keys: `vendor.package.`
314337
*
315338
* @param string $key The namespaced storage key.
316339
* @param mixed $value The value to be stored.
@@ -326,12 +349,12 @@ public static function setState($key, $value)
326349
/**
327350
* Gets information stored bound to the loop.
328351
*
329-
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages.
330-
* Packages MUST use the following prefix to keys: `vendor.package.`
352+
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
353+
* MUST use the following prefix for keys: `vendor.package.`
331354
*
332355
* @param string $key The namespaced storage key.
333356
*
334-
* @return mixed previously stored value or null if it doesn't exist
357+
* @return mixed The previously stored value or `null` if it doesn't exist.
335358
*/
336359
public static function getState($key)
337360
{

src/Loop/Driver.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ abstract public function repeat($interval, callable $callback, $data = null);
8080
/**
8181
* Execute a callback when a stream resource becomes readable or is closed for reading.
8282
*
83+
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
84+
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
85+
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
86+
* resources are therefore undefined behavior.
87+
*
8388
* Multiple watchers on the same stream may be executed in any order.
8489
*
8590
* @param resource $stream The stream to monitor.
@@ -93,6 +98,11 @@ abstract public function onReadable($stream, callable $callback, $data = null);
9398
/**
9499
* Execute a callback when a stream resource becomes writable or is closed for writing.
95100
*
101+
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
102+
* watcher when closing the resource locally. Drivers might choose to notify the user in a debug mode if there are
103+
* watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed
104+
* resources are therefore undefined behavior.
105+
*
96106
* Multiple watchers on the same stream may be executed in any order.
97107
*
98108
* @param resource $stream The stream to monitor.
@@ -106,12 +116,12 @@ abstract public function onWritable($stream, callable $callback, $data = null);
106116
/**
107117
* Execute a callback when a signal is received.
108118
*
109-
* Multiple watchers on the same signal may be executed in any order.
110-
*
111-
* NOTE: Installing the same signal on different instances of this interface is deemed undefined behavior.
119+
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
112120
* Implementations may try to detect this, if possible, but are not required to. This is due to technical
113121
* limitations of the signals being registered globally per process.
114122
*
123+
* Multiple watchers on the same signal may be executed in any order.
124+
*
115125
* @param int $signo The signal number to monitor.
116126
* @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
117127
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
@@ -267,7 +277,7 @@ abstract public function info();
267277
*
268278
* Example: the `uv_loop` resource for `libuv` or the `EvLoop` object for `libev` or `null` for a native driver.
269279
*
270-
* NOTE: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
280+
* Note: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
271281
* instance.
272282
*
273283
* @return null|object|resource The loop handle the event loop operates on. `null` if there is none.

0 commit comments

Comments
 (0)