CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Sporatic fatals when calling crm_update_contact
Pages: [1]

Author Topic: Sporatic fatals when calling crm_update_contact  (Read 2710 times)

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Sporatic fatals when calling crm_update_contact
April 03, 2008, 10:32:08 pm
I'm getting the occasional fatal error (actually in CRM_Core_Error::handle()).  This is very hard to figure out, since the code in question gets called via an XMLRPC handler perhaps 30 times a day;  we'll see two or three fatals in the course of the day.

We're using 1.9 here, under Drupal 5.7.

When the call dies, it looks something like this:
Quote
FATAL CRM ERROR [db_error: message="DB Error: already exists"
 code=-5
mode=callback
callback=CRM_Core_Error::handle prefix=""
info="INSERT INTO civicrm_location (entity_table , entity_id , location_type_id , is_primary ) VALUES ('civicrm_contact' ,  56216 ,  6 ,  0 ) 
[nativecode=1062 ** Duplicate entry 'civicrm_contact-56216-6' for key 2]"]

This should probably be an "INSERT" btw;  I'm not sure why it isn't

The routine that's making the call looks like this:

Quote

//Add on voter info
function &_vfa_append_voter_info(&$contact, $voter_params) {
  _vfa_civicrm_init();
  $contact_id = $contact->id;
  $v_param_list =
    array('street_address','city', 'postal_code', 'state_province_id');
  $params =
    array('country_id' => 1228, //always US
          'contact_id' => $contact_id,
          'location_type' => 'Voting', //voter type
         );
  foreach ($v_param_list as $field) {
    if (isset($voter_params[$field])) {
      $params[$field] = $voter_params[$field];
    }
  }
  $contact =& crm_update_contact($contact, $params);

  return $contact;
}

$contact->id is checked by the calling routine, so it's definitely set.

The stack trace looks something like this:
Quote
DOCUMENT_ROOT/sites/all/modules/civicrm/CRM/Core/Error.php, errorlog_backtrace, 176
, handle,
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/PEAR.php, call_user_func, 901
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB.php, PEAR_Error, 888
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/PEAR.php, DB_Error, 563
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/common.php, raiseError, 1849
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/mysql.php, raiseError, 894
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/mysql.php, mysqlRaiseError, 324
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/common.php, simpleQuery, 1162
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/DataObject.php, query, 2344
DOCUMENT_ROOT/sites/all/modules/civicrm/packages/DB/DataObject.php, _query, 1033
DOCUMENT_ROOT/sites/all/modules/civicrm/CRM/Core/DAO.php, insert, 259
DOCUMENT_ROOT/sites/all/modules/civicrm/api/utils.php, save, 97
DOCUMENT_ROOT/sites/all/modules/civicrm/api/utils.php, _crm_update_object, 997
DOCUMENT_ROOT/sites/all/modules/civicrm/api/Contact.php, _crm_update_contact, 376
DOCUMENT_ROOT/sites/all/modules/tic_user/vfa_fields.inc, crm_update_contact, 324
DOCUMENT_ROOT/sites/all/modules/tic_user/vfa_fields.inc, _vfa_append_voter_info, 130

Any way to call this differently so that it won't crap out on us? 

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Sporatic fatals when calling crm_update_contact
April 03, 2008, 10:42:07 pm
Quote
This should probably be an "INSERT" btw;  I'm not sure why it isn't

Actually, I meant "UPDATE" here.

That, and "sporadic"  :)

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Sporatic fatals when calling crm_update_contact
April 04, 2008, 12:00:51 am

Would it be possible to log the calls and the data you are sending across.  If you have data that causes the fatal error and is reproducible, will enable us (or you) to trace and potentially fix the issue much earlier

The fact that its calling insert, means the code to retrieve the prior location id did not return anything, so i suspect that part has the bug

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Sporatic fatals when calling crm_update_contact
April 04, 2008, 12:14:49 am
I've just added a bit of instrumentation on the calling routine.  I suspect I'll have something to show you within 24 hours.


torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Sporadic fatals when calling crm_update_contact
April 04, 2008, 01:17:39 am
Quote from: Donald Lobo on April 04, 2008, 12:00:51 am

Would it be possible to log the calls and the data you are sending across.  If you have data that causes the fatal error and is reproducible, will enable us (or you) to trace and potentially fix the issue much earlier

The fact that its calling insert, means the code to retrieve the prior location id did not return anything, so i suspect that part has the bug

lobo

Lobo -- I have an example.  Since it's using live data, I'd prefer to send it to you directly.

contact_id is definitely getting sent, and the contact exists, yet CRM generates an INSERT instead of an UPDATE.

I've seen this bug in various guises over the last 3 months, but this is the best I've documented it yet.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Sporatic fatals when calling crm_update_contact
April 04, 2008, 11:06:55 am

ok, next step: can u create an isolated unit test case for this with your current db

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Sporadic fatals when calling crm_update_contact
April 05, 2008, 09:53:14 pm
Quote from: Donald Lobo on April 04, 2008, 11:06:55 am

ok, next step: can u create an isolated unit test case for this with your current db

lobo


Yeah, turns out I can.  Once again, real data, so I'll forward you the actual code.  But with some mods to hide stuff,  the following code will reliably crash for a particular contact, even though it works well in general:
Code: [Select]
civicrm_initialize(TRUE);
$params =
  array(
    'country_id' => 1228,
    'contact_id' => 82389,
    'location_type' => 'Voting',
    'street_address' => '1313 Mockingbird Lane #102',
    'city' => 'Richmond',
    'postal_code' => '22555',
    'state_province_id' => 1045,
  );
$contact_id = 82389;
$params['contact_id'] = $contact_id;
$fetch_params = array('contact_id' => $contact_id);
$contact =& crm_get_contact($fetch_params);
if (!_tic_is_crm_error($contact)) {
  $contact =& crm_update_contact($contact, $params);
}

I've seen variations on this one repeatedly over the last 3 months;  I call update on a contact I've just retrieved or created, and the code blows up.  Most contacts work fine, but a very few have trouble, and will always take CiviCRM 1.9 down deep in the update code.

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Sporatic fatals when calling crm_update_contact
April 06, 2008, 08:45:13 pm
After going a couple of rounds with Lobo, we found the source of the crash.

The cause is a bug in crm_get_contact().  That 1.x API function builds up a contact object for you and returns it, including, you'd think, all of the locations known for the contact.

The bug (in the 1.9 version of the sources) is at line 283.  Here's how it causes a crash in this case:

Quote from: Rob
In crm_get_contact() at line 283,  the call to CRM_Core_BAO_Location::getValues() was hard-coding the number of locations at 2.  It seems pretty safe to change this to 4, since I don't think the main CiviCRM application calls crm_get_contacts() anyway.  At worse, it will affect my [Rob's] own code and one or two other modules I see used.

My client uses 4 locations, and one of the two that was not getting loaded caused crm_update_contact() to assume that it didn't exist when it did.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Sporatic fatals when calling crm_update_contact

This forum was archived on 2017-11-26.