Skip to content

Fix: case when SPOP with count>MAXINT, setTypeRandomElements() will get ... - #2224

Merged
antirez merged 1 commit into
redis:unstablefrom
advance512:spopWithCount
Dec 18, 2014
Merged

antirez merged 1 commit into
redis:unstablefrom
advance512:spopWithCount

Conversation

@advance512

Copy link
Copy Markdown
Contributor

...negative count argument due to signed/unsigned mismatch.

setTypeRandomElements() now returns unsigned long, and also uses unsigned long for anything related to count of members.
spopWithCountCommand() now uses unsigned long elements_returned instead of int, for values returned from setTypeRandomElements()

…et negative count argument due to signed/unsigned mismatch.

setTypeRandomElements() now returns unsigned long, and also uses unsigned long for anything related to count of members.
spopWithCountCommand() now uses unsigned long elements_returned instead of int, for values returned from setTypeRandomElements()
antirez added a commit that referenced this pull request Dec 18, 2014
Fix: case when SPOP with count>MAXINT, setTypeRandomElements() will get ...
@antirez
antirez merged commit efbf5a1 into redis:unstable Dec 18, 2014
@antirez

antirez commented Dec 18, 2014

Copy link
Copy Markdown
Contributor

Thanks, merged.

@mattsta

mattsta commented Dec 20, 2014

Copy link
Copy Markdown
Contributor

The code still needs a lot of cleanup. It's causing memory leaks and has many style problems (end-of-line spaces all over the place, for loops look weird, comments have wrong format, etc). Probably shouldn't have been merged so quickly. 🎄

examples:

==4956== 256 bytes in 16 blocks are definitely lost in loss record 302 of 387
==4956==    at 0x48DBA3: je_malloc (jemalloc.c:944)
==4956==    by 0x4257BD: zmalloc (zmalloc.c:125)
==4956==    by 0x42DD77: createObject (object.c:40)
==4956==    by 0x42E165: createIntsetObject (object.c:207)
==4956==    by 0x43D533: spopWithCountCommand (t_set.c:542)
==4956==    by 0x43D78C: spopCommand (t_set.c:602)
==4956==    by 0x4200ED: call (redis.c:2048)
==4956==    by 0x422F8E: processCommand (redis.c:2308)
==4956==    by 0x42CC7E: processInputBuffer (networking.c:1135)
==4956==    by 0x42CDB1: readQueryFromClient (networking.c:1200)
==4956==    by 0x41AD92: aeProcessEvents (ae.c:412)
==4956==    by 0x41B0AA: aeMain (ae.c:455)
==8719== 16 bytes in 1 blocks are definitely lost in loss record 75 of 355
==8719==    at 0x48DBA3: je_malloc (jemalloc.c:944)
==8719==    by 0x4257BD: zmalloc (zmalloc.c:125)
==8719==    by 0x42DD77: createObject (object.c:40)
==8719==    by 0x42E165: createIntsetObject (object.c:207)
==8719==    by 0x43D533: spopWithCountCommand (t_set.c:542)
==8719==    by 0x43D78C: spopCommand (t_set.c:602)
==8719==    by 0x44E169: loadAppendOnlyFile (aof.c:685)
==8719==    by 0x423D9B: loadDataFromDisk (redis.c:3556)
==8719==    by 0x419CCF: main (redis.c:3753)
==11174== 64 bytes in 1 blocks are definitely lost in loss record 230 of 353
==11174==    at 0x48DBA3: je_malloc (jemalloc.c:944)
==11174==    by 0x4257BD: zmalloc (zmalloc.c:125)
==11174==    by 0x438739: rdbSaveToSlavesSockets (rdb.c:1472)
==11174==    by 0x433DC9: startBgsaveForReplication (replication.c:452)
==11174==    by 0x434A3D: replicationCron (replication.c:2083)
==11174==    by 0x4208FC: serverCron (redis.c:1235)
==11174==    by 0x41AE8D: aeProcessEvents (ae.c:311)
==11174==    by 0x41B0AA: aeMain (ae.c:455)
==11174==    by 0x419D5E: main (redis.c:3776)

@michael-grunder

Copy link
Copy Markdown
Contributor

The first two leaks are I believe due to the fact that createSetObject creates an robj and allocates the ptr as an intset, but freeSetObject will only free the ->ptr member. We could probably just use decrRefCount here, as that will do both.

The final leak is unrelated to this commit, but rather was introduced when diskless replication was merged. This one is due to the fact that msg https://github.com/antirez/redis/blob/unstable/src/rdb.c#L1472 is being allocated but missing a corresponding zfree.

🔬

@mattsta

mattsta commented Dec 20, 2014

Copy link
Copy Markdown
Contributor

Good catch!

We do have tests running at http://ci.redis.io/, but it doesn't send out alerts to anybody, so we don't know when things are broken. 💔

Are you sure about the second one? The "redis-unstable-valgrind" section of http://ci.redis.io/ shows all the errors began with 70674ac, but the msg line has been there since October 23: https://github.com/antirez/redis/blame/unstable/src/rdb.c#L1472

edit: the rdbSaveToSlavesSockets error doesn't actually show up on the CI valgrind for some reason. I just caught it on my local machine.

Maybe old versions of something on the mysterious CI sever?

@michael-grunder

Copy link
Copy Markdown
Contributor

Yeah to be honest I'm not 100% on the second one, it just looks like a leak to me from a cursory inspection (zmalloc but no correpsonding free that I can see).

@advance512

Copy link
Copy Markdown
Contributor Author

Hi all,

Some points:

  1. Yes, there are memory leaks. I asked @mattsta at SPOP optional count argument. (issue #1793) #1803 about this specific issue ("... need to release the strings created by dupStringObject and createStringObjectFromLongLong...") and got a response that made me certain that a memory leak didn't occur, but I was wrong in not verifying it. This was because the original submission was in June, and I was quite out of the context of the contribution by December. Another issue was calling freeSetObject instead of decrRefCount.
  2. I also assumed (in the very same way that I assumed that gcc is ran with -Wextra by default, but it is actually with -Wall and thus doesn't output some warnings) that "make test" uses valgrind by default, hence: no output -> no problem. I was wrong.
  3. Your response, @mattsta, seems a bit harsh considering this is my first contribution to Redis (which isn't a very simple code base), and it isn't a small one either. There is no Redis coding style guide that I could find, no reference to http://ci.redis.io anywhere obvious, nor any specific instructions on the need to build it manually with -Wextra to see all warnings or with "make valgrind" and then to run the tests with "./runtest --valgrind" to get proper valgrind tests. Calling my for loops "weird" (because they are not one-liners) is just plain offensive. Such attitudes are very offputting to potential future contributors.

That being said:

a. I created a new Pull Request #2229 with fixes to all of the issues that @mattsta has raised.
b. It also includes a fix to the rdbSaveToSlavesSockets() memory leak (not related to my changes), which required exactly 2 extra lines of code to correct. (Some extra eyes there would be appreciated: advance512@14b04c0)

All tests pass with flying colours, with valgrind enabled.

I suggest:

(i) The Makefile will be modified to always build with -Wextra.
(ii) When running make test, a valgrind build should be built if necessary ($(MAKE) OPTIMIZATION="-O0" MALLOC="libc") and the --valgrind flag should be enabled by default.
(iii) A coding style guide should be created.
(iv) All of these should be details in the CONTRIBUTING file.

Let me know if anything else requires fixing. Thank you for the comments.

@mattsta

mattsta commented Dec 21, 2014

Copy link
Copy Markdown
Contributor

Thanks for the cleanup!

seems a bit harsh considering this is my first contribution to Redis (which isn't a very simple code base)

I just mentioned the errors again because I thought this PR had fixed the previous issues already. There was a mention of "Cleaned it up as per the Redis required coding style." pointing to this PR. :)

Not a big problem. The feature somehow skipped "review" and went direct to "merge," which seemed odd. I've had every one of your same complaints before too (mysterious hidden CI server, contributors don't have a way of knowing what's good/bad, test suite usage undocumented, etc) but, as we've mentioned, we don't yet have guidelines for any of these things.

I suggest:

Good suggestions! I don't really know why we don't use -Wextra. I compile my travis tests with -Werror too. and, we should have a 'make test-valgrind' target (and accompanying make test-cluster-valgrind, make test-sentinel-valgrind, etc... or maybe just make test-all and make test-all-valgrind).

As far as coding style, the simplest fix is to run clang-format -style="{BasedOnStyle: llvm, IndentWidth: 4}" on new code, but you have to be careful about it re-formatting existing code when submitting new patches since sometimes pre-existing files have inconsistent formatting.

@michael-grunder

Copy link
Copy Markdown
Contributor

@advance512 Thank you for your additional commits! FWIW I took a quick peek and they look good to me. Also you found a second leak! 😺 It's not a small thing getting something into Redis proper, and this is a great addition that will be useful to a lot of people. Cheers!

@advance512

Copy link
Copy Markdown
Contributor Author

I made my remarks about "scaring off" potential future contributors after seeing what happened with other projects. A good example is the Parrot VM that just withered away and died with bad developer attitudes. But: no harm, no foul. I understand the need for maintaining style standards (though my personal style is quite different), and obviously to prevent memory leaks!

To make this more constructive and useful for future contributors, I will try and change the existing Makefile to compile with -Wextra and add the the test-valgrind target (as suggested by @mattsta). Adding some general guidelines and best practices to the CONTRIBUTING file would also be a good idea.

As for clag-format - if it will be used by all contributors prior to every submission, great. Otherwise, as you say, the formatting changes will mask the actual code changes in the commits. How about a bot that clang-formats the entire project automatically every day/week?

Once again, I thank you for your constructive feedback.

@mattsta

mattsta commented Jan 9, 2015

Copy link
Copy Markdown
Contributor

the Parrot VM
no harm, no foul

I see what you did there.

Otherwise, as you say, the formatting changes will mask the actual code changes in the commits.

Those LLVM people are way too smart to let that happen.

They have a script called git-clang-format to only update code changes in individual diffs/commits (anything in your path named git-THING can be run as git THING). It's pretty nifty.

The sad part is, they don't package or release recent versions of clang-format (the package of it on their website is old and doesn't have all the modern options), so you have to build all of LLVM+Clang from source to get a working version.

After you've checked out and built all of LLVM+Clang and put git-clang-format in your path, you can add this to your .git/config and git clang-format will just do the right thing every time:

[clangFormat]
    style="{BasedOnStyle: llvm, IndentWidth: 4, AllowShortFunctionsOnASingleLine: None, KeepEmptyLinesAtTheStartOfBlocks: false}"

How about a bot that clang-formats the entire project automatically every day/week?

The other issue is Redis has some files included directly from other sources, so we wouldn't want format those, or else we can't diff from upstream anymore. That just rules out one big clang-format *.{c,h} — we could always just make a list of "Redis-Only" source to reformat.

Adding some general guidelines and best practices to the CONTRIBUTING file would also be a good idea.

Added to TODO list! (Well, added to the TODO list I keep for prioritizing things to add to the actual TODO list.)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants