Frage

How can a bug could occur on static, fixed input data only "sometimes"? The following is simply an example I have observed, but I am not interested in an answer specific to the GD library.

I have some PHP code to render SVG files, utilizing the GD Graphics library. It often stops working, and no error is given. The strange thing is that after 2-3 tries it works and keeps working with no code changes. This is very strange to me because I understand that assembled code follows in sequential order and operates on certain external inputs. In this case, the known input is a static file.

My question is, how is this possible (from a general, theoretical point of view)? What could the GD library (or any other code) be doing to cause such irregular behavior? How could code run differently based on how many times you ran it, if the input stayed the same?

War es hilfreich?

Lösung

When I understand you correctly, your question is not specificially about GD, but how in general a function with fully deterministic requirements can show non-deterministic behaviour: given exactly the same input, the output (or in this case the behaviour) is different, though there is no obvious cause like a clock-dependency or random generator.

Whenever you run a program or a function on a system, even when the code is executed in a separate process, it is never executed in perfect isolation - there is always some environment around it, lots of other processes running simultanously at the same machine. There are always shared resources like main memory, disk space, processor cores, gfx card memory, global environment variables, and so on, which are not to be expected in exactly the same state when you run the program twice at different points in time. That alone can be the cause of the non-deterministic behaviour you are observing.

Furthermore, GD is written in C, which is a language where one can create "undefined behaviour" in several ways. For example, this can happen accidentally by forgetting to initialize a variable before usage (not that this is really the case for GD, just take this as a hypothetical scenario). This makes it possible the value of a variable can depend on the state of the shared resources I mentioned above. And assumed the GD lib contains such a piece of code somewhere in the SVG rendering code, or in its PHP binding, then it I can imagine that it could lead exactly to the behaviour you described.

Lizenziert unter: CC-BY-SA mit Zuschreibung
scroll top