Infinite loop when adding friends #12

Closed
LeonardChallis opened this Issue Jun 13, 2013 · 6 comments

Comments

Projects
None yet
2 participants

I have a dummy file that inserts random data in to my database to test a system. One part of it is causing the system to hang. See the following code:

$users = UserQuery::create()->find();
foreach ($users as $user){
  $limit = rand(1,count($users)-1);
  $c = new Criteria();
  $c
    ->setLimit($limit)
    ->addAscendingOrderByColumn('rand()')
    ->add('Id', $user->getId(), Criteria::ALT_NOT_EQUAL);
  $randomFriends = UserPeer::doSelect($c);

  $user->addFriends($randomFriends);
  $user->save();
}

This code will hang. From what I can see it's hanging when it reaches the save() function. The weird thing is that it always seems to hang when it reaches user ID 6 (there are 20, PKs in succession from 1 to 20).

Note that when I only add 1 friend (either in an array or just with addFriend() it seems to work fine, and quickly. Also, if I wrap the $user->save() in something like if ($user->getId() < 5) then that also works without hanging, and fairly quickly too. I can also pick higher numbers, not just the first few. The user data is also inserted randomly so it's not that.

Owner

VanTanev commented Jun 13, 2013

I think that you're inserting a user as a friend of himself. Your not equal clause doesn't specify the column name properly - should be UserPeer::ID which has the fully qualified column name tableName.columnName. Try that first.

Yeah I thought it might have been something to do with users adding themselves as friends, but I checked the $randomFriends IDs and they are fine. An example:

1 is adding: 16 2 7 11 18 13 6 19 14 4 5 3 8 10 17 9 12
2 is adding: 11 12 10 4 13 19 16 5 18 9 20 14 1 7 8 3 6
3 is adding: 11 8 4 7 13 12 15 10 16 14
4 is adding: 7 11 14
5 is adding: 7 10 14 20 17 1 19 4 12 11 9 2 6 3 15 13
6 is adding: 4 1 18 14 16 5 2 12 20 13 7 15 11 19
7 is adding: 13 17 20 3 10 11
8 is adding: 3 10 11 1 19 4 12 16 18 9 2 13 6 5 14
9 is adding: 6 3 16 10 20 14 5 7 19 11 2 12 13 8 17 15 18 4 1
10 is adding: 20 16 9 2 5 6 12 18 1 7 4
11 is adding: 16 6
12 is adding: 4 7 11 10 13
13 is adding: 9 8 2 7 19 17 3 18 12 6 16 14 20 10 1 5 4
14 is adding: 13 20 19 6 7 4 15 1 8 17 3
15 is adding: 12 1 4 19
16 is adding: 17 18 9 4 7 1 2 13 12 10
17 is adding: 2 5 18 4 16 13 7 10
18 is adding: 14 6 16 9 10
19 is adding: 13 17 11 6 7
20 is adding: 3 7 12 2 4 18 17 5 13 1 16 14 19 9 10 8
Owner

VanTanev commented Jun 13, 2013

I really don't have much to go on here. If you could provide me with more context, or the actual code, I could go through it with a step-by-step debugger and try to figure out what's happening.

I've simplified it in to a very small project and put it here: https://github.com/LeonardChallis/EqualNestBehaviorTest

By the way on a (probably) unrelated note, to get propel-gen sql to work for me I had to add:

include_once __DIR__ . DIRECTORY_SEPARATOR . 'EqualNestParentBehavior.php';
include_once __DIR__ . DIRECTORY_SEPARATOR . 'EqualNestParentBehaviorObjectBuilderModifier.php';
include_once __DIR__ . DIRECTORY_SEPARATOR . 'EqualNestParentBehaviorQueryBuilderModifier.php';

to the top of EqualNestBehavior.php. I'm not sure if this is because of my current Windows environment (global Composer install) or an actual bug.

Owner

VanTanev commented Jun 13, 2013

I ran it; It seems to me that it's not an actual infinite loop, just some really bad optimization for those particular calls you're making. The object call graph gets out of hand pretty quickly when relations start calling save() on their relations.

I'll see if there is something I can do about that though.

Yeah that makes sense, thanks for taking a look. For now I will just use custom SQL and insert in to the friend table the IDs.

VanTanev closed this in ba45a47 Jun 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment