Infinite loop in php code

Are you absolutely, positively sure that your question does not fit anywhere else?
cliffus
Posts: 5
Joined: Wed Oct 09, 2013 8:45 am
Location: Ghent, Belgium
Contact:

Infinite loop in php code

Postby cliffus » Wed Oct 09, 2013 9:06 am

Hi

I'm targetting my question to the scientiamobile support desk.

Before buying a commercial license, we are doing some tests with the self-hosted WURFL php project (http://sourceforge.net/projects/wurfl/f ... RFL%20PHP/ version 1.5.0).

Because free users don't have access to the up-to-date version of the device database, we are using the free and outdated version of the database. During our tests, we have noticed that device detection didn't work at all for iOS7 devices. After doing some research, we discovered that PHP gets out of memory because of an infinite loop in the WURFL PHP code, probably because the free database doesn't contain any information about iOS7 yet.

Our apache error logs contains thousands of these php notices and gets out of memory:

Code: Select all

PHP Notice:  Trying to get property of non-object in /var/www/..../vendor/wurfl-php-1.5.0.0/WURFL/CustomDeviceRepository.php on line 209
...
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /var/www/.../vendor/wurfl-php-1.5.0.0/WURFL/CustomDeviceRepository.php on line 208

Code: Select all

/**
	 * Returns an array containing all devices from the root
	 * device to the device of the given $deviceId
	 *
	 * @param string $deviceId
	 * @return array All WURFL_Device objects in the fallback tree
	 */
	public function getDeviceHierarchy($deviceId) {
		$devices = array();
		while (strcmp($deviceId, "root")) {
			$device = $this->getDevice($deviceId);
			$devices[] = $device;
/* 209 */		$deviceId = $device->fallBack;
		}
		return $devices;
	}
The fact that our demo setup doesn't recognise iOS7 devices isn't really an issue for us, this will be fixed when we have the commercial license. But in case WURFL doesn't recognise some kind of exotic device, we don't want it to crash our website! It's very important for us that WURFL works without having any critical issues in it.

Thanks for your support.

Cliff
Cliff Ophalvens

Lead Android developer
www.inthepocket.mobi

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: Infinite loop in php code

Postby kamermans » Wed Oct 09, 2013 9:36 am

Hi Cliff,

While we haven't released a new public WURFL snapshot, the v1.5 API does ship with a brand-new WURFL file, so you're welcome to use that.

With regards to the infinite loop, this is due to a missing 'generic' device. I can patch the code to avoid the loop, but it will still throw an exception since a missing 'generic' device is an irrecoverable error. This scenario is only possible when the persistence layer is volatile, for example, if you use Memcache for persistence, it's possible that the 'generic' device was pushed out of memory. For this reason we recommend using the filesystem for persistence and something like Memcache or APC for cache. Note that in this configuration, nearly 100% of filesystem I/O is cached.
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!

cliffus
Posts: 5
Joined: Wed Oct 09, 2013 8:45 am
Location: Ghent, Belgium
Contact:

Re: Infinite loop in php code

Postby cliffus » Wed Oct 09, 2013 9:44 am

Hi

thanks for your quick response, I'll try the database shipped with the latest snapshot and let you know.

We are using MySQL as persistence layer, so the generic device should be in there somewhere. I think an exception would be better than an infinite loop, because then we can handle these situations without having an out of memory issue.
Cliff Ophalvens

Lead Android developer
www.inthepocket.mobi

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: Infinite loop in php code

Postby kamermans » Wed Oct 09, 2013 9:51 am

Sure, no problem. Please let me know if you are able to reproduce this issue. I will also test it on my side with MySQL as a persistence layer.

Do you have any caching enabled?
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!

cliffus
Posts: 5
Joined: Wed Oct 09, 2013 8:45 am
Location: Ghent, Belgium
Contact:

Re: Infinite loop in php code

Postby cliffus » Wed Oct 09, 2013 9:59 am

By using the latest database, the issue is fixed for iOS 7 devices (but will still probably occur with other unknown devices).

This is my configuration file:

Code: Select all

<wurfl-config>
	<wurfl>
		<main-file>wurfl.zip</main-file>
	</wurfl>
	<allow-reload>true</allow-reload>
	<match-mode>performance</match-mode>
	<capability-filter>
		<capability>is_wireless_device</capability>
		<capability>brand_name</capability>
		<capability>model_name</capability>
		<capability>marketing_name</capability>
		<capability>is_wireless_device</capability>
		<capability>is_tablet</capability>
		<capability>device_os</capability>
		<capability>device_os_version</capability>
		<capability>resolution_width</capability>
		<capability>resolution_height</capability>
		<capability>ux_full_desktop</capability>
		<capability>pointing_method</capability>
		<capability>mobile_browser</capability>
		<capability>mobile_browser_version</capability>
		<capability>preferred_markup</capability>
		<capability>xhtml_support_level</capability>

	</capability-filter>
	<persistence>
		<provider>mysql</provider>
		<params>host=localhost,port=3306,db=wurfl,user=wurfl,pass=wurfl,table=persistence</params>
	</persistence>
	<cache>
		<provider>mysql</provider>
		<params>host=localhost,port=3306,db=wurfl,user=wurfl,pass=wurfl,table=cache</params>
	</cache>
</wurfl-config>
I was using database version 2.3.4 (http://sourceforge.net/projects/wurfl/f ... RFL/2.3.4/)

thanks!
Cliff Ophalvens

Lead Android developer
www.inthepocket.mobi

cliffus
Posts: 5
Joined: Wed Oct 09, 2013 8:45 am
Location: Ghent, Belgium
Contact:

Re: Infinite loop in php code

Postby cliffus » Wed Oct 09, 2013 10:12 am

If needed, this was my index.php file:

Code: Select all

<html>
<head>
	<title>WURFL PHP API Example</title>
</head>
<body>
<?php
// Include the configuration file
include_once '../conf/wurfl_config_xml.php';

$wurflInfo = $wurflManager->getWURFLInfo();

// This line detects the visiting device by looking at its HTTP Request ($_SERVER)
$requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);

?>
<h3>WURFL XML INFO</h3>
<ul>
	<li><h4>VERSION: <?php echo $wurflInfo->version; ?> </h4></li>
</ul>
<div id="content">
	User Agent: <b> <?php echo htmlspecialchars($_SERVER['HTTP_USER_AGENT']); ?> </b>
	<ul>
		<li>ID: <?php echo $requestingDevice->id; ?> </li>
		<li>Brand Name: <?php echo $requestingDevice->getCapability('brand_name'); ?> </li>
		<li>Model Name: <?php echo $requestingDevice->getCapability('model_name'); ?> </li>
		<li>Marketing Name: <?php echo $requestingDevice->getCapability('marketing_name'); ?> </li>
		<li>Is mobile: <?php echo $requestingDevice->getVirtualCapability('is_mobile'); ?> </li>
		<li>Is smartphone: <?php echo $requestingDevice->getVirtualCapability('is_smartphone'); ?> </li>
		<li>Is tablet: <?php echo $requestingDevice->getCapability('is_tablet'); ?> </li>
		<li>Is full desktop: <?php echo $requestingDevice->getVirtualCapability('is_full_desktop'); ?> </li>
		<li>Is android: <?php echo $requestingDevice->getVirtualCapability('is_android'); ?> </li>
		<li>Is iOS: <?php echo $requestingDevice->getVirtualCapability('is_ios'); ?> </li>
		<li>Is Windows Phone: <?php echo $requestingDevice->getVirtualCapability('is_windows_phone'); ?> </li>
		<li>Device OS: <?php echo $requestingDevice->getCapability('device_os'); ?> </li>
		<li>Device OS version: <?php echo $requestingDevice->getCapability('device_os_version'); ?> </li>
		<li>Resolution Width: <?php echo $requestingDevice->getCapability('resolution_width'); ?> </li>
		<li>Resolution Height: <?php echo $requestingDevice->getCapability('resolution_height'); ?> </li>
	</ul>
</div>
</body>
</html>
Cliff Ophalvens

Lead Android developer
www.inthepocket.mobi

kamermans
Posts: 393
Joined: Mon Jun 06, 2011 9:50 am

Re: Infinite loop in php code

Postby kamermans » Wed Oct 09, 2013 10:20 am

I have found the problem.

Issue #1: the WURFL version you are using is much older than the API, and the API is hardcoded with some device IDs that must be present. For this reason, you must use a WURFL that is as new as, or newer than, the API. We will try to make this requirement more obvious, so I apologize for this oversight.

Issue #2: WURFL_Storage_Mysql::load() returns (bool)false instead of null, which causes WURFL_CustomDeviceRepository::getCapabilityForDevice() to believe the device was recognized, where it should be throwing an exception on line 158.

I have fixed the API and we will release an update shortly.
Thanks,

Steve Kamerman
ScientiaMobile

Make sure you check out our WURFL Cloud, WURFL InSight and WURFL InFuze products!

cliffus
Posts: 5
Joined: Wed Oct 09, 2013 8:45 am
Location: Ghent, Belgium
Contact:

Re: Infinite loop in php code

Postby cliffus » Thu Oct 10, 2013 3:29 am

thanks a lot!
Cliff Ophalvens

Lead Android developer
www.inthepocket.mobi


Who is online

Users browsing this forum: No registered users and 13 guests